1446. Consecutive Characters

1446. Consecutive Characters #

题目 #

  • 字符串的**「能量」**定义为:只包含一种字符的最长非空子字符串的长度。

  • 返回字符串 s能量

思路 #

模拟 #

代码 #

模拟 #

class Solution {
    public int maxPower(String s) {
        int maxPower = 1, power = 1;
        char ch = s.charAt(0);
        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i) == ch) { power += 1; maxPower = Math.max(power, maxPower); }
            else { power += 1; ch = s.charAt(i); }
        }
        return maxPower;
    }
}