Solved

java challenge concatenation

Posted on 2014-11-14
12
263 Views
Last Modified: 2014-11-20
Hi,

I am working on below coding challenge
http://codingbat.com/prob/p143825
i wrote as below


public String nonStart(String a, String b) {
  String result=null;
  String aFirst=null;
  String bFirst=null;
 
  if(a.length()>1) {
  result=a.substring(1,a.length());
  }
  else{
 
  return "";
  }
  if(b.length()>1) {
  result+=b.substring(1,b.length());
  }
 
  return result;
}


My test cases are passing
i would like to know how can improve on my above code. Please advise.Thanks in advance
0
Comment
Question by:gudii9
12 Comments
 
LVL 16

Accepted Solution

by:
krakatoa earned 325 total points
ID: 40442490
Shorter would be :

public String nonStart(String a, String b) {
  return a.substring(1)+b.substring(1);
}

Open in new window

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 75 total points
ID: 40442527
your code is not passing
nonStart("x", "ac") → "c"      ""      X
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40442541
your code

would always be good for everyone if we knew who you are referring to each time. I remember we had this ID issue once before.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40442563
@gudii -

You don't need to test the lengths, that's a waste of time.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40442822
i do not need to test lengths since they said strings are atleast of length 1 right in the challenge?


nonStart("x", "ac") → "c"      ""      X

I wonder why code i wrote is failing above test. please advise
public String nonStart(String a, String b) {
  String result=null;
  String aFirst=null;
  String bFirst=null;
  
  if(a.length()>1) {
  result=a.substring(1,a.length());
  }
  else{
  
  return "";
  }
  if(b.length()>1) {
  result+=b.substring(1,b.length());
  }
 
  return result;
}

Open in new window


Expected	Run		
nonStart("Hello", "There") → "ellohere"	"ellohere"	OK	    
nonStart("java", "code") → "avaode"	"avaode"	OK	    
nonStart("shotl", "java") → "hotlava"	"hotlava"	OK	    
nonStart("ab", "xy") → "by"	"by"	OK	    
nonStart("ab", "x") → "b"	"b"	OK	    
nonStart("x", "ac") → "c"	""	X	    
nonStart("a", "x") → ""	""	OK	    
nonStart("kit", "kat") → "itat"	"itat"	OK	    
nonStart("mart", "dart") → "artart"	"artart"	OK	    
other tests
OK	    

Open in new window

when  a string is length 1 it failed. when b string is length 1 no issues. I thought if it failed when a is length 1 then it should fail test cases when b is length 1. please advise
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40442891
The substring of an empty string is an empty string.

You don't need to test the length of the strings, because the method feeds you the strings into your code - all you need to do is to capture all of both strings you are given,  MINUS the first character in each, and put them together. If there is no first character in the first string, (i.e. there is no string at all), then "nothing" gets added to the second string after the second string is stripped of its first character. And if the second string also has no first character (so it's blank), then you return an empty string, which is fine.

If one of them or both have 1 or fewer characters, then an empty string is returned.

If a or b or both has more than 1 character (i.e. 2 or more), then you return the two substrings concatenated after character 0 of each until the end of each string.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 16

Expert Comment

by:krakatoa
ID: 40442969
And so here is your code again, corrected to that end :

public String nonStart(String a, String b) {
  //String result=null;
 //String aFirst=null;
  //String bFirst=null;
  
  //if(a.length()>1) {
  //result=a.substring(1,a.length());
  //}
  //else{
  
  //return "";
  //}
  //if(b.length()>1) {
  //result+=b.substring(1,b.length());
  //}
 
  return a.substring(1)+b.substring(1);

}

Open in new window

0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 100 total points
ID: 40444464
gudii9,
I answered this before in question Q_28562074.html, which said if the length of string a = 1 and the length of string b > 1, then just return the substring of b, and if length of string b = 1 and the length of string a > 1, then just return the substring of a, and if the lengths of both a and b are > 1, then return the concatenation of the substrings of a and b., else return null.

krakatoa,
>>If one of them or both have 1 or fewer characters, then an empty string is returned<<
I don't think this is correct. If one of them has 1 character and the other is greater than 1, it should return the substring of the one that was greater than 1.

All,
I believe this is correct and what seems to be missing from the other solutions.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40444466
krakatoa,
 >>If one of them or both have 1 or fewer characters, then an empty string is returned<<
 I don't think this is correct. If one of them has 1 character and the other is greater than 1, it should return the substring of the one that was greater than 1.

. . .  you've got to re-read what you have said here, and see that it doesn't make any sense. I said that if one of them or both have 1 or fewer characters  . . . . . . . so why have you said that " . .  and the other is greater than 1, . ." ??

I'd actually prefer to hear from the OP - gudii - on this before we become entangled any further. gudii : all you need to do is to understand why I commented-out nearly all of your code in your last post, and left in just one line with the return value. This question is far simpler than you seem to think it is, so please just re-read the question, and compare it to what is going on in that return statement I have given you.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40445073
(Correction :

In my post ID: 40442891

I said "The substring of an empty string is an empty string." That's true only when you take a substring of an empty string at index 0. Otherwise you'll get an out of bounds exception if you try any + or - index.)
0
 
LVL 7

Author Comment

by:gudii9
ID: 40455259
public String nonStart(String a, String b) {
  //String result=null;
 //String aFirst=null;
  //String bFirst=null;
  
  //if(a.length()>1) {
  //result=a.substring(1,a.length());
  //}
  //else{
  
  //return "";
  //}
  //if(b.length()>1) {
  //result+=b.substring(1,b.length());
  //}
 
  return a.substring(1)+b.substring(1);

}

Open in new window


i liked above solution. simple and straight forward. Basically returning substring on a and b starting index 1 till  all the way end.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40455457
Right. Pleased it met with your approval.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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