Solved

matchUp  challenge

Posted on 2016-08-30
9
105 Views
Last Modified: 2016-09-07
I am working on below challenge
http://codingbat.com/prob/p139677

Psedo code:
1. loop first array
2. loop second array within
3. check to see first element of first array with first element of second  then second then third till end
4. if matches increase counter by 1
4. return counter

I wrote my code as below

public int matchUp(String[] a, String[] b) {
  int count=0;
  for(int i=0;i<a.length;i++){
     for(int j=0;j<b.length;j++){
       if(a[i]==b[i]){
         count++;
       }
     }
    
  }
  return count;
}

Open in new window





I am not passing all tests
Expected      Run            
matchUp(["aa", "bb", "cc"], ["aaa", "xx", "bb"]) → 1      0      X      
matchUp(["aa", "bb", "cc"], ["aaa", "b", "bb"]) → 2      0      X      
matchUp(["aa", "bb", "cc"], ["", "", "ccc"]) → 1      0      X      
matchUp(["", "", "ccc"], ["aa", "bb", "cc"]) → 1      0      X      
matchUp(["", "", ""], ["", "bb", "cc"]) → 0      3      X      
matchUp(["aa", "bb", "cc"], ["", "", ""]) → 0      0      OK      
matchUp(["aa", "", "ccc"], ["", "bb", "cc"]) → 1      0      X      
matchUp(["x", "y", "z"], ["y", "z", "x"]) → 0      0      OK      
matchUp(["", "y", "z"], ["", "y", "x"]) → 1      6      X      
matchUp(["x", "y", "z"], ["xx", "yyy", "zzz"]) → 3      0      X      
matchUp(["x", "y", "z"], ["xx", "yyy", ""]) → 2      0      X      
matchUp(["b", "x", "y", "z"], ["a", "xx", "yyy", "zzz"]) → 3      0      X      
matchUp(["aaa", "bb", "c"], ["aaa", "xx", "bb"]) → 1      3      X      
other tests

How to improve/modify my design, code and any other alternate approaches. please advise
0
Comment
Question by:gudii9
9 Comments
 
LVL 3

Assisted Solution

by:Donna
Donna earned 250 total points
ID: 41777465
Wow, look at all these. Fall semester must have started, LOL

You can't use == with strings I don't think...I think you need to use stringcomp() , or better yet, .equals() method ...look in the API for methods in the String class and comparing strings. == does not work with STrings, only sometimes.

read up on compareTo() , compareToIgnoreCase() , equals(), equalsIgnoreCase(); , contentEquals(CharSequence cs); , ...and whatever else looks close to what you want to do. But probably equals() will work for you, it returns a bool.

https://docs.oracle.com/javase/7/docs/api/
0
 
LVL 7

Author Comment

by:gudii9
ID: 41780954
public int matchUp(String[] a, String[] b) {
  int count=0;
  for(int i=0;i<a.length;i++){
     for(int j=0;j<b.length;j++){
       if(a[i].equals(b[j])){
         count++;
       }
     }
    
  }
  return count;
}

Open in new window


i used equals passed some tests and failing few

Expected      Run            
matchUp(["aa", "bb", "cc"], ["aaa", "xx", "bb"]) → 1      1      OK      
matchUp(["aa", "bb", "cc"], ["aaa", "b", "bb"]) → 2      1      X      
matchUp(["aa", "bb", "cc"], ["", "", "ccc"]) → 1      0      X      
matchUp(["", "", "ccc"], ["aa", "bb", "cc"]) → 1      0      X      
matchUp(["", "", ""], ["", "bb", "cc"]) → 0      3      X      
matchUp(["aa", "bb", "cc"], ["", "", ""]) → 0      0      OK      
matchUp(["aa", "", "ccc"], ["", "bb", "cc"]) → 1      1      OK      
matchUp(["x", "y", "z"], ["y", "z", "x"]) → 0      3      X      
matchUp(["", "y", "z"], ["", "y", "x"]) → 1      2      X      
matchUp(["x", "y", "z"], ["xx", "yyy", "zzz"]) → 3      0      X      
matchUp(["x", "y", "z"], ["xx", "yyy", ""]) → 2      0      X      
matchUp(["b", "x", "y", "z"], ["a", "xx", "yyy", "zzz"]) → 3      0      X      
matchUp(["aaa", "bb", "c"], ["aaa", "xx", "bb"]) → 1      2      X      
other tests

please advise
0
 
LVL 27

Accepted Solution

by:
rrz earned 250 total points
ID: 41786775
The challenge states:
Count the number of times that the 2 strings are non-empty and start with the same char.
 You are checking for equality of the two elements.  You need to check for empty Strings and not to count them. You need to find the first character of the String and check their equality.  Look at
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#charAt-int-     
Also, you only need one loop. Just step through both in one pass.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 7

Author Comment

by:gudii9
ID: 41787167
public int matchUp(String[] a, String[] b) {
  int count=0;
  for(int i=0;i<a.length;i++){
    // for(int j=0;j<b.length;j++){
       if(a[0].equals(b[0])){
         count++;
     //  }
     }
    
  }
  return count;
}
/*The challenge states:
Count the number of times that the 2 strings are non-empty and start with the same char.
 You are checking for equality of the two elements.  You need to check for empty Strings 
 and not to count them. You need to find the first character of the String and check 
 their equality.  Look at 
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#charAt-int-     
Also, you only need one loop. Just step through both in one pass.*/

Open in new window


something like above. It passes some and fails some tests
Expected      Run            
matchUp(["aa", "bb", "cc"], ["aaa", "xx", "bb"]) → 1      0      X      
matchUp(["aa", "bb", "cc"], ["aaa", "b", "bb"]) → 2      0      X      
matchUp(["aa", "bb", "cc"], ["", "", "ccc"]) → 1      0      X      
matchUp(["", "", "ccc"], ["aa", "bb", "cc"]) → 1      0      X      
matchUp(["", "", ""], ["", "bb", "cc"]) → 0      3      X      
matchUp(["aa", "bb", "cc"], ["", "", ""]) → 0      0      OK      
matchUp(["aa", "", "ccc"], ["", "bb", "cc"]) → 1      0      X      
matchUp(["x", "y", "z"], ["y", "z", "x"]) → 0      0      OK      
matchUp(["", "y", "z"], ["", "y", "x"]) → 1      3      X      
matchUp(["x", "y", "z"], ["xx", "yyy", "zzz"]) → 3      0      X      
matchUp(["x", "y", "z"], ["xx", "yyy", ""]) → 2      0      X      
matchUp(["b", "x", "y", "z"], ["a", "xx", "yyy", "zzz"]) → 3      0      X      
matchUp(["aaa", "bb", "c"], ["aaa", "xx", "bb"]) → 1      3      X      
other tests
X
0
 
LVL 7

Author Comment

by:gudii9
ID: 41787171
public int matchUp(String[] a, String[] b) {
  int count=0;
  for(int i=0;i<a.length;i++){
    String s1=a[i];
    String s2=b[i];
    // for(int j=0;j<b.length;j++){
    if(!s1.equals("")&&!s2.equals("")){
       if( s1.charAt(0)==s2.charAt(0) ){
         count++;
     //  }
       }
     }
    
  }
  return count;
}

/*The challenge states:
Count the number of times that the 2 strings are non-empty and start with the same char.
 You are checking for equality of the two elements.  You need to check for empty Strings 
 and not to count them. You need to find the first character of the String and check 
 their equality.  Look at 
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#charAt-int-     
Also, you only need one loop. Just step through both in one pass.*/

Open in new window


you mean like above right. Above passes all tests. Any improvements or alternate approaches for this?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41787201
Yes, your code is good.
My code is almost the same as your code.
public int matchUp(String[] a, String[] b) {
  int count=0;
  for(int i=0;i<a.length;i++){
       if(a[i].length() != 0 && b[i].length() != 0 && a[i].charAt(0) == b[i].charAt(0)){
         count++;
       }
  }
  return count;
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41787778
yours is more optimized. thanks
0
 
LVL 32

Expert Comment

by:awking00
ID: 41788139
Good place to use continue -
int count=0;
  for(int i=0;i<a.length;i++){
       if (a.isEmpty || b.isEmpty) {
           continue;
       }
       if ( a[i].charAt(0) == b[i].charAt(0) ) {
         count++;
       }
  }
0
 
LVL 7

Author Comment

by:gudii9
ID: 41788960
i like above continue approach

if one of below  non empty ie false (if both empty continue to next element of iteration right?)

a.isEmpty || b.isEmpty


then it checks below and increments count++ if first characters equal??
   if ( a[i].charAt(0) == b[i].charAt(0) ) {
         count++;
       }

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question