endOther ava challenge

gudii9
gudii9 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

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

Most Valuable Expert 2014
Top Expert 2015
Commented:
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());

Author

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

Author

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());

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial