Solved

matchUp  challenge

Posted on 2016-08-30
9
72 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 31

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
A short article about problems I had with the new location API and permissions in Marshmallow
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

13 Experts available now in Live!

Get 1:1 Help Now