Solved

min concatination challenge

Posted on 2015-01-12
12
94 Views
Last Modified: 2015-01-14
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.
0
Comment
Question by:gudii9
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 300 total points
Comment Utility
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

by:gudii9
Comment Utility
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
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Is the Math.max necessary?
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Dunno for Java, but in most languages negative string positions result in errors.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
Comment Utility
Is it possible for aLen-minLen or bLen-minLen to be negative?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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

0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 100 total points
Comment Utility
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);
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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

by:zzynx
Comment Utility
right?
Right! Instead of your two IFs.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thanx 4 axxepting
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Printing to a specific printer tray (HP 806dn printer) 3 62
How To Loop - Python 19 63
JAVA part two 5 39
bitbucket vs gitbucket 3 29
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

763 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

9 Experts available now in Live!

Get 1:1 Help Now