class Solution: def maxPower(self, s: str) -> int: if set(s) == s: return 1 if len(s)==1: return 1 count = 1 res=[] i=1 while i<len(s): if s[i] == s[i-1]: i+=1 count+=1 else: count=1 i+=1 res.append(count) return max(res) class Solution: def maxPower(self, s: str) -> int: if set(s) == s: return 1 if len(s)==1: return 1 count = 1 Max =1 i=1 while i<len(s): if s[i] == s[i-1]: i+=1 count+=1 else: count=1 i+=1 Max = max(Max,count) return Max class Solution: def maxPower(self, s: str) -> int: left , right = 0,0 res =1 while left<=right<len(s): if s[left]==s[right]: right+=1 res = max(res,right-left) else: left = right return res
难度简单2收藏分享切换为英文关注反馈
给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。
请你返回字符串的能量。
示例 1:
输入:s = "leetcode" 输出:2 解释:子字符串 "ee" 长度为 2 ,只包含字符 'e' 。示例 2:
输入:s = "abbcccddddeeeeedcba" 输出:5 解释:子字符串 "eeeee" 长度为 5 ,只包含字符 'e' 。示例 3:
输入:s = "triplepillooooow" 输出:5示例 4:
输入:s = "hooraaaaaaaaaaay" 输出:11示例 5:
输入:s = "tourist" 输出:1