zl程序教程

您现在的位置是:首页 >  其他

当前栏目

每日一题---1446. 连续字符[力扣][Go]

2023-03-14 22:59:42 时间

题目描述

给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。

请你返回字符串的能量。

解题代码

// 滑动窗口
func maxPower(s string) int {
    res := 0
    fIndex := 0
    aIndex := 0
    for aIndex < len(s) {
        if s[aIndex] == s[fIndex] {
            aIndex ++
        } else {
            if res < aIndex - fIndex {
                res = aIndex - fIndex
            }
            fIndex = aIndex
        }
    }
    if res < aIndex - fIndex {
        res = aIndex - fIndex
    }
    return res
}

提交结果

image