Solved

matchUp  challenge

Posted on 2016-08-30
9
133 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hive vs Impla in Hadoop 1 146
statistics basics sources 2 73
Mutiple Folder view (5 Folder view) 3 81
Does it make sense to split a controller into two file 6 18
A short article about problems I had with the new location API and permissions in Marshmallow
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

734 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