Java > String-2 > prefixAgain (CodingBat Solution)

Problem:

Given a string, consider the prefix string made of the first N chars of the string. Does that prefix string appear somewhere else in the string? Assume that the string is not empty and that N is in the range 1..str.length().

prefixAgain("abXYabc", 1) → true
prefixAgain("abXYabc", 2) → true
prefixAgain("abXYabc", 3) → false


Solution:

public boolean prefixAgain(String str, int n) {
  int len = str.length();
  String prefix = str.substring(0,n);
  
  for (int i = n; i < len; i++) {
    if(n+i <= len) { 
      if (prefix.equals(str.substring(i,n+i)))
        return true;
    }
  }
  return false;
}


20 comments :

  1. Smaller:

    public boolean prefixAgain(String str, int n) {
    return str.lastIndexOf(str.substring(0, n)) != 0;
    }

    ReplyDelete
  2. or:

    public boolean prefixAgain(String str, int n) {
    return (str.substring(n).indexOf(str.substring(0,n)) != -1);
    }

    ReplyDelete
  3. public boolean prefixAgain(String str, int n) {
    String substr = str.substring(n);
    String pattern = str.substring(0, n);

    return substr.contains(pattern);
    }

    ReplyDelete
  4. public boolean prefixAgain(String str, int n) {
    return str.matches(str.substring(0, n) + "(.*)" + str.substring(0, n) + "(.*)");
    }

    ReplyDelete
  5. public boolean prefixAgain(String str, int n) {
    return str.indexOf(str.substring(0, n), n) > -1;
    }

    ReplyDelete
  6. public boolean prefixAgain(String str, int n) {
    return str.substring(n).contains(str.substring(0,n));
    }

    ReplyDelete
  7. public boolean prefixAgain(String str, int n) {

    for(int i=0; i<str.length()-n; i++)
    {
    if(str.substring(0,n).equals(str.substring(i+1,i+n+1)))
    return true;
    }

    return false;
    }

    ReplyDelete
  8. public boolean prefixAgain(String str, int n) {
    return str.substring(n).contains(str.substring(0,n));
    }

    ReplyDelete
  9. public boolean prefixAgain(String str, int n) {
    String result = str.substring(n);
    String check = str.substring(0, n);
    return result.contains(check);
    }

    ReplyDelete
  10. public boolean prefixAgain(String str, int n) {
    int sep = 0;
    for (int i = n ; i <= str.length() - n ; i++){
    if(str.substring(0,n).equals(str.substring(i,i+n))){
    sep = str.substring(n,i+n).length();
    return str.substring(0,n).equals(str.substring(sep, sep + n));
    }
    }
    return false;

    }

    ReplyDelete
  11. public boolean prefixAgain(String str, int n) {
    int len = str.length();
    String prefix = str.substring(0,n);

    if(str.substring(n,len).contains(prefix))
    return true;

    return false;
    }

    ReplyDelete
  12. public boolean prefixAgain(String str, int n) {
    String number = str.substring(0,n);
    String newStr = str.substring(n);

    if(newStr.contains(number)){
    return true;
    } else {
    return false;
    }
    }

    ReplyDelete
  13. public boolean prefixAgain(String str, int n) {
    String checkString = str.substring(0, n);
    String others=str.substring(n,str.length());
    if (others.contains(checkString)) {
    return true;
    } else {
    return false;
    }
    }

    ReplyDelete
  14. public boolean prefixAgain(String str, int n) {
    if (n > str.length() / 2)
    {
    return false;
    }
    else
    {
    for (int i = str.length() - n; i > n - 1; i--)
    {
    if (str.substring(i, i + n).equals(str.substring(0, n)))
    {
    return true;
    }
    }
    return false;
    }
    }

    ReplyDelete
  15. public boolean prefixAgain(String str, int n) {
    String prefix = str.substring(0,n);
    String nextPart = str.substring(n);
    return nextPart.contains(prefix);
    }

    ReplyDelete
  16. single line code:
    public boolean prefixAgain(String str, int n) {
    return str.substring(str.indexOf(str.substring(0,n))+1)
    .contains(str.substring(0,n));
    }

    ReplyDelete
  17. public boolean xyzMiddle(String str) {
    int countLeft = 0;
    int countRight = 0;
    int size = str.length();
    String tar = "xyz";
    String acc = "";

    if(size < 3){
    return false;
    }
    for(int i = 0; i < size - 2; i++){
    acc = "";
    // Traverse 3chars at a time, compare to xyz each step
    acc = acc + str.charAt(i) + str.charAt(i + 1) + str.charAt(i + 2);
    if(!acc.equals(tar)){
    countLeft += 1;
    }
    else{
    // Capture index where char z of xyz is found
    int z = i + 2;
    // Store count of remaining letters after xyz, size - 1 - found index z
    countRight = size - 1 - z;
    }
    }
    countLeft = countLeft - countRight;
    int diff = countLeft - countRight;
    if(diff == 1 || diff == 0 || diff == -1){
    return true;
    }
    return false;
    }

    ReplyDelete
  18. public boolean prefixAgain(String str, int n) {
    String acc = "";
    String comp = "";

    for(int i = 0; i < n; i++){
    acc = acc + str.charAt(i);
    }
    for(int i = n; i <= str.length() - n; i++){
    comp = "";
    for(int j = i; j < i + n; j++){
    comp = comp + str.charAt(j);
    }
    if(acc.equals(comp)){
    return true;
    }
    }
    return false;
    }

    ReplyDelete
  19. Another way

    public boolean prefixAgain(String str, int n) {
    return (str.split(str.substring(0, n), - 1).length - 1) > 1;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact