prefixAgain java challenge

Hi,
I am working on below challenge
http://codingbat.com/prob/p136417
I tried my code as below
public boolean prefixAgain(String str, int n) {

 String strNew = str.substring(0, n - 1);
 int len = str.length();
 String strLaterHalf = str.substring(n, len);
 if (strLaterHalf.contains(strNew)) {

  return true;
 } else {
  return false;
 }

}

Open in new window

I am getting below result
Expected      Run            
prefixAgain("abXYabc", 1) → true      true      OK         
prefixAgain("abXYabc", 2) → true      true      OK         
prefixAgain("abXYabc", 3) → false      true      X         
prefixAgain("xyzxyxyxy", 2) → true      true      OK         
prefixAgain("xyzxyxyxy", 3) → false      true      X         
prefixAgain("Hi12345Hi6789Hi10", 1) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 2) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 3) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 4) → false      true      X         
prefixAgain("a", 1) → false      true      X         
prefixAgain("aa", 1) → true      true      OK         
prefixAgain("ab", 1) → false      true      X         
other tests
X         

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?
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.

krakatoaCommented:
return str.substring(1).contains(str.substring(0,n));

Open in new window

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
krakatoaCommented:
Here is your code, reworked, btw :

public boolean prefixAgain(String str, int n) {
  String strNew = str.substring(0, n);
 int len = str.length();
 String strLaterHalf = str.substring(1, len);
 if (strLaterHalf.contains(strNew)) {

  return true;
 } else {
  return false;
 }
}

Open in new window

ozoCommented:
String strLaterHalf = str.substring(n, len);
and
 String strLaterHalf = str.substring(1, len);
would interpret the problem specification differently on
prefixAgain("aaa", 2)
That does not seem to be one of the "other tests", so the ambiguity remains unresolved.
My preference would be for return str.substring(1).contains(str.substring(0,n));
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

krakatoaCommented:
My preference would be for return str.substring(1).contains(str.substring(0,n));

Which is what I put in my first comment.
ozoCommented:
Presumably that's why you changed n to 1 in gudii9's code, so that it would be consistent with yours, even though that change was not necessary in order to pass the tests.
krakatoaCommented:
Well I felt that using the 1 would help in seeing the fundamental mechanics of moving that pointer - rather than bringing n into it again, as it tends to "overload" n's use case, whereas if gudii asks "why is there a 1 there", then potential ambiguity can be avoided.

I am also unsure whether the question really intends to avoid overlapping or not, so 1 seemed better.
ozoCommented:
We seem to share the intuition that substring(1) is somehow a more fundamental interpretation of the problem statement than substring(n).
Yet we see that other intuitions are possible, and we lack the evidence to say that one is correct and one is incorrect.
Other challenges on the codingbat site seem to have similar ambiguities, and the tests similarly seem to fail to resolve the ambiguity, which may suggest the lack of resolution is deliberate.
krakatoaCommented:
which may suggest the lack of resolution is deliberate.

Pleased you qualified that statement with the word "may". Because I would have thought that the very worst place for demonstrating ambiguity, is in a teaching/learning environment. This, imho, is even worse when there are no hints or visible caveats telling the user that they should be cautious in drawing conclusions about the overall definitiveness of obtaining a "passed all tests" result.

This weakness has shown itself up several times in these challenges, and to me leaves the door wide-open to the criticism that "a little knowledge is a dangerous thing". I wouldn't want these people to churn out programmers for coding my driverless car or fbw aircraft, nor any number of other criticial procedures where a proper understanding of the task was essential, rather than just the quick-and-dirty fingers of trigger-happy hackers. Perhaps CB has a retinue of lawyers ready behind the scenes somewhere.
awking00Information Technology SpecialistCommented:
I think the choice of str.substring(1,len) [or just str.substring(1)] as being the "better" choice is based on CodingBat's description of "Does that prefix string appear somewhere else in the string?" as opposed to "... appear later in the string?" Of course that is still a matter of interpretation and they don't give an example that demonstrates their intent.
gudii9Author Commented:
public boolean prefixAgain(String str, int n) {

String strNew=str.substring(0,n);
int len=str.length();
String strLaterHalf=str.substring(n, len);
if( strLaterHalf.contains(strNew)){

return true;
}

else{
return false;
}
  
}

//

Open in new window


i see mistake i did is added -1 unnecessarily. above passing all tests
gudii9Author Commented:
public class PrefixAgainEx {

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

	
	public static boolean prefixAgain(String str, int n) {

		String strNew=str.substring(0,n);
		System.out.println("strNew is-->"+strNew);
		int len=str.length();
		String strLaterHalf=str.substring(n, len);
		System.out.println("strLaterHalf is-->"+strLaterHalf);
		if( strLaterHalf.contains(strNew)){

		return true;
		}

		else{
		return false;
		}
		  
		}

}

Open in new window


above give below correctr result
strNew is-->abX
strLaterHalf is-->Yabc
Values is-->false
gudii9Author Commented:
public class PrefixAgainEx {

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

	
	public static boolean prefixAgain(String str, int n) {

		String strNew=str.substring(0,n);
		System.out.println("strNew is-->"+strNew);
		int len=str.length();
		String strLaterHalf=str.substring(n, len);
		System.out.println("strLaterHalf is-->"+strLaterHalf);
		if( strLaterHalf.contains(strNew)){

		return true;
		}

		else{
		return false;
		}
		  
		}

}

Open in new window


above passed the test of  prefixAgain("aaa", 2));
      }


My preference would be for return str.substring(1).contains(str.substring(0,n));



I wonder why we choose 1 in above solution why not 2 or 3?? just to be safer side that n is always there 1 or more??
ozoCommented:
return str.substring(2).contains(str.substring(0,n)); is clearly wrong in this case:
Expected      Run
prefixAgain("aa", 1) → true      false
gudii9Author Commented:
package simple.servlet;
public class PrefixAgainEx {

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

	
	public static boolean prefixAgain(String str, int n) {

		String strNew=str.substring(0,n);
		System.out.println("strNew is-->"+strNew);
		int len=str.length();
		String strLaterHalf=str.substring(n, len);
		System.out.println("strLaterHalf is-->"+strLaterHalf);
		if( strLaterHalf.contains(strNew)){

		return true;
		}

		else{
		return false;
		}
		  
		}

}

Open in new window


i got result as true when i run same as expected
strNew is-->a
strLaterHalf is-->a
Values is-->true
gudii9Author Commented:
public boolean prefixAgain(String str, int n) {

return str.substring(2).contains(str.substring(0,n));
}


above failed two test cases as you mentioned

Expected      Run            
prefixAgain("abXYabc", 1) → true      true      OK         
prefixAgain("abXYabc", 2) → true      true      OK         
prefixAgain("abXYabc", 3) → false      false      OK         
prefixAgain("xyzxyxyxy", 2) → true      true      OK         
prefixAgain("xyzxyxyxy", 3) → false      false      OK         
prefixAgain("Hi12345Hi6789Hi10", 1) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 2) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 3) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 4) → false      false      OK         
prefixAgain("a", 1) → false      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)      X         
prefixAgain("aa", 1) → true      false      X         
prefixAgain("ab", 1) → false      false      OK         
other tests
OK         
gudii9Author Commented:
public boolean prefixAgain(String str, int n) {

return str.substring(1).contains(str.substring(0,n));
}

Open in new window


above passed all tests

Expected      Run            
prefixAgain("abXYabc", 1) → true      true      OK         
prefixAgain("abXYabc", 2) → true      true      OK         
prefixAgain("abXYabc", 3) → false      false      OK         
prefixAgain("xyzxyxyxy", 2) → true      true      OK         
prefixAgain("xyzxyxyxy", 3) → false      false      OK         
prefixAgain("Hi12345Hi6789Hi10", 1) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 2) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 3) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 4) → false      false      OK         
prefixAgain("a", 1) → false      false      OK         
prefixAgain("aa", 1) → true      true      OK         
prefixAgain("ab", 1) → false      false      OK         
other tests
OK
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.