-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1446.py
47 lines (37 loc) · 1008 Bytes
/
p1446.py
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
import unittest
# 维护窗口
class Solution1:
@staticmethod
def maxPower(s: str) -> int:
ans = 0
l = 0
# 维护终点
for r in range(len(s)):
# 维护起点
if s[r] != s[l]:
l = r
else:
ans = max(ans, r - l + 1)
return ans
# 分组循环
class Solution2:
@staticmethod
def maxPower(s: str) -> int:
ans = 0
i = 0
# 维护起点
while i < len(s):
start = i
i += 1
# 维护终点
while i < len(s) and s[i] == s[i - 1]:
i += 1
ans = max(ans, i - start)
return ans
class Test(unittest.TestCase):
def test(self) -> None:
self.assertEqual(Solution1.maxPower("leetcode"), 2)
self.assertEqual(Solution1.maxPower("abbcccddddeeeeedcba"), 5)
self.assertEqual(Solution1.maxPower("ccbccbb"), 2)
if __name__ == "__main__":
unittest.main()