Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# min concatination challenge

Posted on 2015-01-12
Medium Priority
110 Views
Hi,

I am trying below challenge

http://codingbat.com/prob/p105745

My code is like below.
``````public String minCat(String a, String b) {
int aLen=a.length();
int bLen=b.length();
if(aLen==bLen){
return a+b;
}

if(aLen>bLen){
return a+b;
}

if(aLen<bLen){
return a+b;
}
return null;
}
``````

i am failing all test cases. Please advise on how to modify and improve my code.

0
Question by:gudii9
• 5
• 3
• 2
• +1

LVL 71

Accepted Solution

Qlemo earned 1200 total points
ID: 40544648
You are always just concatenating the two strings, so of course that fails.
You need to take the smaller size, and use only the last n characters of the longer string.

If I would write the code, I would not care about checking which string is longer. I would do something like (in pseudo code):
minsize = minimum of aLen and bLen
return minsize chars from end of a and minsize chars from end of b

That works for all cases.
0

LVL 7

Author Comment

ID: 40545241
``````String minCat(String a, String b) {
int aLen=a.length();
int bLen=b.length();
int minSize=0;
String s="";
if(aLen==bLen){
s=a+b;
return s;
}
//s1.compareTo(s)

/*You are always just concatenating the two strings, so of course that fails.
You need to take the smaller size, and use only the last n characters of the longer string.

If I would write the code, I would not care about checking which string is longer. I would do something like (in pseudo code):
minsize = minimum of aLen and bLen
return minsize chars from end of a and minsize chars from end of b

That works for all cases.*/

if(aLen>bLen){
//return minSize=bLen.length();
s= a.substring(aLen-bLen)+b.substring(bLen-bLen);
return s;
}

if(aLen<bLen){
s= a.substring(aLen-aLen)+b.substring(bLen-aLen);
}
return s;
}
``````

Like above?

``````public class Test41 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s=	minCat("at","hi");
System.out.println("s is-->"+s);

}

public static String minCat(String a, String b) {
int aLen=a.length();
int bLen=b.length();
int minSize=0;
String s="";
if(aLen==bLen){
s=a+b;
return s;
}
//s1.compareTo(s)

/*You are always just concatenating the two strings, so of course that fails.
You need to take the smaller size, and use only the last n characters of the longer string.

If I would write the code, I would not care about checking which string is longer. I would do something like (in pseudo code):
minsize = minimum of aLen and bLen
return minsize chars from end of a and minsize chars from end of b

That works for all cases.*/

if(aLen>bLen){
//return minSize=bLen.length();
s= a.substring(aLen-bLen)+b.substring(bLen-bLen);
return s;
}

if(aLen<bLen){
s= a.substring(aLen-aLen)+b.substring(bLen-aLen);
}
return s;
}
}
``````

Now i was able to pass all the tests. How can i improve my code.
0

LVL 71

Expert Comment

ID: 40545379
``````int minLen = Math.min(aLen, bLen);
return a.substring(Math.max(0, aLen-minLen)) + b.substring(Math.max(0, bLen-minLen));
``````
is what I was talking about
0

LVL 85

Expert Comment

ID: 40545492
Is the Math.max necessary?
0

LVL 71

Expert Comment

ID: 40545514
Dunno for Java, but in most languages negative string positions result in errors.
0

LVL 85

Assisted Solution

ozo earned 400 total points
ID: 40545529
Is it possible for aLen-minLen or bLen-minLen to be negative?
0

LVL 71

Expert Comment

ID: 40545547
No :-\ Got you, we can and should omit the max.
``````int minLen = Math.min(aLen, bLen);
return a.substring(aLen-minLen) + b.substring(bLen-minLen);
``````
0

LVL 37

Assisted Solution

zzynx earned 400 total points
ID: 40548393
So gudii9, in case you can't follow, in short all the above comments result in this simple method:

``````public String minCat(String a, String b) {
int aLen = a.length(), bLen = b.length();
int minLen = Math.min(aLen, bLen);
return a.substring(aLen-minLen) + b.substring(bLen-minLen);
}
``````
0

LVL 7

Author Comment

ID: 40549023
i see instead of two if loops like i wrote now we can do in one return statement with substring as we already got minLen from Math.min function right?
0

LVL 71

Expert Comment

ID: 40549031
You didn't have any loop. You used three IFs to catch all cases. That or the mathematical approach like shown by me are the common solutions.
0

LVL 37

Expert Comment

ID: 40549061
right?
0

LVL 37

Expert Comment

ID: 40549205
Thanx 4 axxepting
0

## Featured Post

Question has a verified solution.

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

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the bâ€¦
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and â€¦
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month10 days, 4 hours left to enroll

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

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