Solved

matchUp  challenge

Posted on 2016-08-30
9
91 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Definitions and default visual studio colors 5 58
egit plugin on eclipse 8 28
servlet example issue 6 29
"Black Box" Testing of Control System Software 2 17
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

929 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now