给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。
请你返回字符串 s 的 能量。
示例 1:
输入:s = “leetcode”
输出:2
解释:子字符串 “ee” 长度为 2 ,只包含字符 ‘e’ 。
示例 2:
输入:s = “abbcccddddeeeeedcba”
输出:5
解释:子字符串 “eeeee” 长度为 5 ,只包含字符 ‘e’ 。
题解
循环往后遍历,记录连续出现最多的次数
/**
* @param {string} s
* @return {number}
*/
var maxPower = function(s) {
let len = s.length,num = 1,count=1
for(let a = 0; a < len; a++){
if(s[a] === s[a+1]){
num++
}else{
count = Math.max(num,count)
num = 1
}
}
return count
};