【题目】
给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。请你返回字符串的能量。
来源:leetcode
链接:https://leetcode-cn.com/problems/consecutive-characters/
【示例】
输入:s = “leetcode”
输出:2
解释:子字符串 “ee” 长度为 2 ,只包含字符 ‘e’
【示例2】
输入:s = “abbcccddddeeeeedcba”
输出:5
解释:子字符串 “eeeee” 长度为 5 ,只包含字符 ‘e’
【示例3】
输入:s = “triplepillooooow”
输出:5
【示例4】
输入:s = “hooraaaaaaaaaaay”
输出:11
【示例5】
输入:s = “tourist”
输出:1
【代码】
class Solution {
public:
int maxPower(string s) {
char ch=' ';
int maxlen=0,cnt=0;
for(auto x:s){
if(x!=ch){
cnt=0;
ch=x;
}
cnt++;
maxlen=max(cnt,maxlen);
}
return maxlen;
}
};