endOther ava challenge

Hi,
I am working on below challenge
http://codingbat.com/prob/p126880
I tried my code as below
public boolean endOther(String a, String b) {
  int aLen=a.length();
  int bLen=b.length();
  
 int diff=aLen-bLen;
 int absDiff=Math.abs(diff);
 
 if(a.substring(absDiff)==b||b.substring(absDiff)==a){
 return true;
 }
 else{
 
 return false;
 }
}

Open in new window

I am getting below result
Expected      Run            
endOther("Hiabc", "abc") → true      false      X         
endOther("AbC", "HiaBc") → true      false      X         
endOther("abc", "abXabc") → true      false      X         
endOther("Hiabc", "abcd") → false      false      OK         
endOther("Hiabc", "bc") → true      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:8)      X         
endOther("Hiabcx", "bc") → false      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:8)      X         
endOther("abc", "abc") → true      true      OK         
endOther("xyz", "12xyz") → true      false      X         
endOther("yz", "12xz") → false      false      OK         
endOther("Z", "12xz") → true      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:8)      X         
endOther("12", "12") → true      true      OK         
endOther("abcXYZ", "abcDEF") → false      false      OK         
endOther("ab", "ab12") → false      false      OK         
endOther("ab", "12ab") → true      false      X         
other tests
X         
Your progress graph for this problem

how to  improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?
 Please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gudii9Author Commented:
public boolean endOther(String a, String b) {
 int aLen = a.length();
 int bLen = b.length();

 int diff = aLen - bLen;
 int absDiff = Math.abs(diff);

 if ((a.substring(absDiff, aLen).equals(b)) || (b.substring(absDiff, bLen).equals(a))) {
  return true;
 } else {

  return false;
 }
}

Open in new window


i updated and only failing two now.

Expected      Run            
endOther("Hiabc", "abc") → true      true      OK         
endOther("AbC", "HiaBc") → true      false      X         
endOther("abc", "abXabc") → true      true      OK         
endOther("Hiabc", "abcd") → false      false      OK         
endOther("Hiabc", "bc") → true      true      OK         
endOther("Hiabcx", "bc") → false      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:8)      X         
endOther("abc", "abc") → true      true      OK         
endOther("xyz", "12xyz") → true      true      OK         
endOther("yz", "12xz") → false      false      OK         
endOther("Z", "12xz") → true      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:8)      X         
endOther("12", "12") → true      true      OK         
endOther("abcXYZ", "abcDEF") → false      false      OK         
endOther("ab", "ab12") → false      false      OK         
endOther("ab", "12ab") → true      true      OK         
other tests
X         
Correct for more than half the tests

Your progress graph for this problem


public class endOtherEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("value--->"+endOther("Hiabc", "abc"));

	}
	
	public static boolean endOther(String a, String b) {
		  int aLen=a.length();
		  int bLen=b.length();
		  
		 int diff=aLen-bLen;
		 int absDiff=Math.abs(diff);
		 
		 if((a.substring(absDiff,aLen).equals(b))||(b.substring(absDiff,bLen).equals(a))){
		 return true;
		 }
		 else{
		 
		 return false;
		 }
		}

}

Open in new window

ozoCommented:
int alen=a.length();
  int blen=b.length();
  int minlen=Math.min(alen,blen);
  return a.substring(alen-minlen,alen).toLowerCase().equals(b.substring(blen-minlen,blen).toLowerCase());

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
public boolean endOther(String a, String b) {
  int aLen = a.length();// 6
		int bLen = b.length();// 2

		int diff = aLen - bLen;
		int absDiff = Math.abs(diff);

if ((aLen >= bLen && a.substring(absDiff, aLen).equalsIgnoreCase(b))) {
			//System.out.println("11");
			return true;
			// ||(b.substring(absDiff,bLen).equals(a)
		}

else if ((bLen >= aLen && b.substring(absDiff, bLen).equalsIgnoreCase(a))) {
			//System.out.println("22");
			return true;
		} else {

			return false;
		}
		}

Open in new window


above passed all tests

package simple.servlet;

public class EndOtherEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// System.out.println("values is--->"+endOther("Hiiabc", "bc"));
		System.out.println("values is--->" + endOther("bc", "Hiiabc"));
	}

	public static boolean endOther(String a, String b) {
		int aLen = a.length();// 6
		int bLen = b.length();// 2

		int diff = aLen - bLen;
		int absDiff = Math.abs(diff);// 4
		System.out.println("aLen is-->" + aLen);
		System.out.println("bLen is-->" + bLen);
		// System.out.println("a.substring(absDiff,aLen) is-->"+a.substring(absDiff-1));//
		// 3
		System.out.println("b.substring(absDiff,bLen) values isssss-->"
				+ b.substring(absDiff, bLen));
		System.out.println("b.substring(absDiff,bLen).equals(a) is-->"
				+ b.substring(absDiff, bLen).equals(a));
		if ((aLen >= bLen && a.substring(absDiff, aLen).equals(b))) {
			System.out.println("11");
			return true;
			// ||(b.substring(absDiff,bLen).equals(a)
		}

		else if ((bLen >= aLen && b.substring(absDiff, bLen).equals(a))) {
			System.out.println("22");
			return true;
		} else {

			return false;
		}
	}

}

Open in new window

gudii9Author Commented:
i see finding minlen avoided if and else if and else loops like mine.

which scenarios we should find the minlen?? what logic behind finding minlen here??

int alen=a.length();
  int blen=b.length();
  int minlen=Math.min(alen,blen);
  return a.substring(alen-minlen,alen).toLowerCase().equals(b.substring(blen-minlen,blen).toLowerCase());
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.