更快地重复字符计数方法

时间:2018-11-19 15:36:58

标签: java

我目前正在为Twitch聊天机器人创建审核系统,因此我采用了这种方法来查找重复字符的最长序列,但由于这种方法,我不知道哪个字符或它将在哪里重复将用于检查随机消息。谁能帮我简化下面的代码并使之永久化?

public int getLongestLetterSequence(String message) {
    int biggestRepeatingCount = 0;
    int totalRepeatingCharacters = 0;
    char currentRepeatingCharacter = message.charAt(0);

    for (int i = 0; i < message.length(); i++) {
        char c = message.charAt(i);

        if (Character.isLetterOrDigit(c)) {
            if (c == currentRepeatingCharacter) {
                biggestRepeatingCount++;
            } else {
                if (totalRepeatingCharacters < biggestRepeatingCount) {
                    totalRepeatingCharacters = biggestRepeatingCount;
                }
                biggestRepeatingCount = 0;
            }
        }
        currentRepeatingCharacter = c;
    }

    return totalRepeatingCharacters + 1;
}

2 个答案:

答案 0 :(得分:1)

这是基于正则表达式的解决方案,比您当前的方法要简洁得多。它尝试使用以下模式查找重复序列:

(.)(\\1)+

这表示要匹配并捕获任何单个字符,然后该相同字符将被跟踪一次或多次。我们会跟踪运行时间最长的重复序列,然后在最后显示。

String input = "The quick brown bbb fox jumps over the aaaaaa lazy dog zzz.";

String longest = "";
String pattern = "(.)(\\1)+";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);

while (m.find()) {
    if (m.group(0).length() > longest.length()) {
        longest = m.group(0);
    }
}

System.out.println("Longest sequence: " + longest);

Longest sequence: aaaaaa

此正则表达式解决方案的优点是,它可能比您当前的方法更容易阅读。但是,仅使用基本字符串函数的解决方案实际上可能比此方法运行得更快。

答案 1 :(得分:1)

我想出一些办法来提高性能(并在您的代码中发现一个错误)。

错误

您具有以下代码:

if (c == currentRepeatingCharacter) {
    biggestRepeatingCount++;
} else {
    if (totalRepeatingCharacters < biggestRepeatingCount) {
        totalRepeatingCharacters = biggestRepeatingCount;
    }
    biggestRepeatingCount = 0;
}

但是,如果totalRepeatingCharacters看到一个不等于currentRepeatingCharacter的字符,并且将仅设置 ,并且 this 成为问题:

    String string =
            "aaaaaaaaaaaaaaaaaaaaaaasidhihasdbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";

它为此24找到长度String,它与"aaaaaaaaaaaaaaaaaaaaaaa".length() + 1的长度相同。如前所述,数字只会在找到另一个字符后更新,即使b字符的数量更大,也不会跟随另一个字符,因此也不会注册。

修复

if语句移动到另一个位置,如下所示:

if (c == currentRepeatingCharacter) {
    biggestRepeatingCount++;
    if (totalRepeatingCharacters < biggestRepeatingCount) {
        totalRepeatingCharacters = biggestRepeatingCount;
    }
} else {
    biggestRepeatingCount = 0;
}

性能提升

当您遍历String时,由于String中剩余的字符数小于totalRepeatingCharacters,因此最终可以停止,因此{{1} }将会增加(已经找到最大值)

totalRepeatingCharacters

我们仅在切换到新角色时才检查这种情况。对于某些if (c == currentRepeatingCharacter) { biggestRepeatingCount++; if (totalRepeatingCharacters < biggestRepeatingCount) { totalRepeatingCharacters = biggestRepeatingCount; } } else { if(string.length() - i <= maxRepeatingCount + 1) { if (totalRepeatingCharacters < biggestRepeatingCount) { totalRepeatingCharacters = biggestRepeatingCount; } break; } biggestRepeatingCount = 0; } ,代码现在的执行时间更短:

String

当到达字符String string = "aaaaaaaaaaaaaaaaaaaaaaasidhxhazdbbbbbbbbbbbbbbbbbbbb"; 时,它将h移出break循环,因为for不可能比现在更大。