Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

min concatination challenge

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;
}

Open in new window


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

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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;
		}

Open in new window


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;
		}
}

Open in new window


Now i was able to pass all the tests. How can i improve my code.
Please advise
int minLen = Math.min(aLen, bLen);
return a.substring(Math.max(0, aLen-minLen)) + b.substring(Math.max(0, bLen-minLen)); 

Open in new window

is what I was talking about
Is the Math.max necessary?
Dunno for Java, but in most languages negative string positions result in errors.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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?
please advise
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.
right?
Right! Instead of your two IFs.
Thanx 4 axxepting