-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1209.py
37 lines (26 loc) · 1 KB
/
m1209.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
class Solution:
def removeDuplicates(self, s: str, k: int) -> str:
stk = [] # (letter, count)
stkAlt = []
for c in s :
if stk and c == stk[-1][0] :
stk[-1] = (c, stk[-1][1] + 1)
else :
stk.append((c, 1))
if stk[-1][1] == k :
stk.pop()
while stk :
c, freq = stk.pop()
if len(stk) >= 1 and c == stk[-1][0] : # merge
stk[-1] = (c, stk[-1][1] + freq)
continue
if freq % k == 0 : # remove all substrs
if stkAlt :
stk.append(stkAlt.pop())
continue
if freq > k : # remove parts above
stk.append((c, freq % k))
continue
stkAlt.append((c, freq))
stk, stkAlt = stkAlt, stk
return ''.join([x[0] * x[1] for x in reversed(stk)])