// 解法1:官方推荐
const maxPower = (s) => {
let ans = 1;
let cnt = 1;
for (let i = 1; i < s.length; i++) {
if (s[i] === s[i - 1]) {
cnt++;
ans = Math.max(ans, cnt);
} else {
cnt = 1;
}
}
return ans;
};
// 解法2:双指针
const maxPower2 = (s) => {
let ans = 1;
let cnt = 1;
for (let i = 0, j = 1; j < s.length; j++) {
if (s[i] === s[j]) {
cnt++;
ans = Math.max(ans, cnt);
} else {
i = j;
cnt = 1
}
}
return ans;
}
其实两种解法大同小异,个人更推荐官方解法,不论是在时间还是在空间上都更胜一筹