leetcode-1446.连续字符
2021年12月01日74 次阅读0 人喜欢
技术算法leetcode
1446.连续字符
给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。 请你返回字符串的能量。
示例1
输入:s = "leetcode"
输出:2
解释:子字符串 "ee" 长度为 2 ,只包含字符 'e' 。
示例2
输入:s = "triplepillooooow"
输出:5
javascript
var maxPower = function (s) {
var max = 1
var currentLength = 1
var index = 0;
var arr = s.split('');
while (index <= arr.length - 1) {
if (arr[index] === arr[index + 1]) {
currentLength++
if (currentLength > max) {
max = currentLength
}
} else {
currentLength = 1
}
index++
};
return max
};