简体   繁体   English

使用 Java 中的字符串库在给定字符串中 substring 的最大连续重复次数

[英]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.我试图在给定的字符串中找到 substring 的最大连续重复。 I'm using substring(), equals(), and length() methods from the String library.我正在使用 String 库中的 substring()、equals() 和 length() 方法。 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"让例如dna= "CAGCAGCAGTTCAGCAGCAGCAGTTCAGCAGCAG"

Then, max consecutive repeats of substring "CAG" = 4 ---> expected output然后,最大连续重复 substring "CAG" = 4 ---> 预期 output

But for this substring or any substring, this is the result I get-但是对于这个 substring 或任何 substring,这是我得到的结果-

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".当 substring 不等于“CAG”时,它被覆盖为 0。 Instead you only need to set the value of count=0 in else condition and not max =0.相反,您只需要在 else 条件下设置 count=0 的值,而不是 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.问题是您正在比较不等于CAG并且没有必要,导致您无法正确保存最大值。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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