给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。
请你返回字符串的能量。
引申题目:leetcode1180 https://leetcode-cn.com/problems/count-substrings-with-only-one-distinct-letter/
示例 1:
输入:s = "leetcode"
输出:2
解释:子字符串 "ee" 长度为 2 ,只包含字符 'e' 。
示例 2:
输入:s = "abbcccddddeeeeedcba"
输出:5
解释:子字符串 "eeeee" 长度为 5 ,只包含字符 'e' 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/consecutive-characters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
哈哈,刚巧考到 itertools模块的groupby函数模块,灵感大爆发!
class Solution:
def maxPower(self, s: str) -> int:
res = 0
for _,j in itertools.groupby(s):
list2 = len(list(j))
if list2>res:
res =list2
else:
continue
return res
if __name__ == "__main__":
s =Solution()
s2 = "leetcode"
print(s.maxPower(s2))