-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1446.py
More file actions
67 lines (52 loc) · 1.36 KB
/
1446.py
File metadata and controls
67 lines (52 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/6/1 16:54
# @Author : Fhh
# @File : 1446.py
# Good good study,day day up!
"""
给你一个字符串 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
提示:
1 <= s.length <= 500
s 只包含小写英文字母。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/consecutive-characters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
"""
class Solution:
def maxPower(self, s: str) -> int:
if not s:
return 0
m = 1
i = 1
k = 0
while k + i < len(s):
if s[k + i] == s[k]:
i += 1
else:
m = max(m, i)
k += i
i = 1
return max(m, i)
s = Solution()
print(s.maxPower('leeeetcooooodeeeeeeeee'))