1446. 连续字符

  1. 连续字符
    给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。

请你返回字符串的能量。

示例 1:

输入:s = "leetcode"
输出:2
解释:子字符串 "ee" 长度为 2 ,只包含字符 'e' 。

示例 2:

输入:s = "abbcccddddeeeeedcba"
输出:5
解释:子字符串 "eeeee" 长度为 5 ,只包含字符 'e' 。

示例 3:

输入:s = "triplepillooooow"
输出:5
class Solution {
public:
    int maxPower(string s) {
        int len = s.size();
        int left = 0, res = INT_MIN;
        while(left < len){
            int right = left + 1;
            while(right < len && s[right] == s[left]){
                ++right;
            }
            res = max(res, right - left);
            left = right;
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_43599304/article/details/121649640
今日推荐