Loading

【leetcode】1446. Consecutive Characters

The power of the string is the maximum length of a non-empty substring that contains only one unique character.

Given a string s, return the power of s.

Example 1:

Input: s = "leetcode"
Output: 2
Explanation: The substring "ee" is of length 2 with the character 'e' only.

    统计字符串中最长的连续字符串长度,利用双指针进行维护。

class Solution {
public:
    int maxPower(string s) {
        // 双指针
        int res=0;
        int low=0,fast=0;
        while(low<s.size()){
            while(s[low]==s[fast]){
                fast++;
            }
            res=max(res,fast-low);
            //cout<<low<<fast<<endl;
            low=fast;
        }
        return res;     
    }
};

 

posted @ 2021-12-13 23:59  aalanwyr  阅读(85)  评论(0)    收藏  举报