有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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;
}

共 (0) 个答案