Avatar of gudii9
Flag for United States of America

asked on 

prefixAgain java challenge

I am working on below challenge
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

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
Java EEJavaProgramming Languages-OtherProgramming

Avatar of undefined
Last Comment

8/22/2022 - Mon