-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsecutive_characters.py
52 lines (33 loc) · 1.3 KB
/
consecutive_characters.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
48
49
50
51
52
'''
The power of the string is the maximum length of a non-empty substring that contains only one unique character.
Given a string s, return the power of s.
'''
class Solution:
def maxPower(self, s: str) -> int:
def f(x): return [list(group) for c, group in itertools.groupby(x)]
s = f(s)
maxi = 0
for i in range(len(s)):
if len(s[i]) > maxi:
maxi = len(s[i])
print(maxi)
return maxi
#more classical solution
class Solution:
def maxPower(self, s: str) -> int:
# the minimum value for consecutive is 1
local_max, global_max = 1, 1
# dummy char for initialization
prev = '#'
for char in s:
if char == prev:
# keeps consecutive, update local max
local_max += 1
# update global max length with latest one
global_max = max( global_max, local_max )
else:
# lastest consective chars stops, reset local max
local_max = 1
# update previous char as current char for next iteration
prev = char
return global_max