LeetCode 1446 Consecutive Characters (Python)

Posted by 小明MaxMing on November 3, 2020

题目

Given a string s, the power of the string is the maximum length of a non-empty substring that contains only one unique character.

Return the power of the string.

解题思路

遍历字符串,统计当前字符重复出现的次数,遇到新的字符从1开始

代码

class Solution:
    def maxPower(self, s: str) -> int:
        res = ct = 1
        for i in range(1, len(s)):
            if s[i] != s[i - 1]:
                res = max(res, ct)
                ct = 1
            else:
                ct += 1
        return max(res, ct)

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅