简体   繁体   中英

Maximum consecutive repeats of a substring in a given string using string library in Java

I'm trying to find the maximum consecutive repeats of a substring in a given string. I'm using substring(), equals(), and length() methods from the String library. However, I don't get the correct result. Here's my code-

 public static int maxRepeats(String dna) {
        int max = 0;
        int count = 0;
        for (int i = 0; i < dna.length() - 3; i++) {
            String s = dna.substring(i, i + 3);
            if (s.equals("CAG")) {
                count++;
                i += 2;
            }
            if (!s.equals("CAG")) {
                max = count;
                count = 0;
            }
        }
        return max;
    }

Let for example dna= "CAGCAGCAGTTCAGCAGCAGCAGTTCAGCAGCAG"

Then, max consecutive repeats of substring "CAG" = 4 ---> expected output

But for this substring or any substring, this is the result I get-

max repeats = 0

Would be grateful if someone pointed out where am I wrong:-)

The problem with your code was you are not saving the max value properly. It was getting overridden to value 0 when ever the substring is not equal to "CAG". Instead you only need to set the value of count=0 in else condition and not max =0. Check this code. It should work for you

int max = 0;
int count = 0;
for (int i = 0; i < dna.length() - 3; i++) {
     String s = dna.substring(i, i + 3);
      if (s.equals("CAG")) {
          count++;
          i += 2;
       } else {
          count=0;
       }
            
       if (count>max) {
          max = count;
        }
}

The problem is you are comparing for not equal to CAG and it is not necessary, resulting in you not saving the max correctly. Also, it is not necessary to check for count on each iteration.

public static int maxRepeats(String dna) {
    int max = 0;
    int count = 0;
    for (int i = 0; i <= dna.length() - 3; i++) {
        String s = dna.substring(i, i + 3);
        if (s.equals("CAG")) {
            count++;
            i += 2;
        } else {
            // only check max when CAG is not present.
            if (count > max) {
                max = count;
            }
            // but reset counter regardless.
            count = 0;
        }
    }
    return Math.max(max,count);
}

Another alternative is to use regular expressions.

public static int maxRepeats(String dna) {
    // find the longest repeats of CAG
    Matcher m = Pattern.compile("(CAG)*").matcher(dna);
    int longest = 0;
    while (m.find()) {
        String f = m.group();
        if (f.length() > longest) {
            longest = f.length();
        }
    }
    // the longest string must be divisible by 3 so...
    return longest/3;
}

Method 1:

/**
* @param pattern string being searched
* @param text string being searched in
* @return max number of times pattern can be self-appended and remains a 
* substring of text
*/
int maxRepeats(String pattern, String text) {
    int max = 0;
    int count = 0;
    int i = 0;
    while (i <= text.length() - pattern.length()) {
        String s = text.substring(i, i + pattern.length());
        if (s.equals(pattern)) {
            count++;
            i += pattern.length();
        } else {
            max = Math.max(max, count);
            count = 0;
            i++;
        }
    }
    return Math.max(max, count);
}

Method 2:

int maxRepeats(String pattern, String text) {
    String s = pattern;
    int max = 0;
    while (s.length() <= text.length()) {
        if (text.contains(s)) {
            max++;
            s += pattern;
        } else {
            break;
        }
    }
    return max;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM