Skip to content

Commit 82a2573

Browse files
authored
Create remove-all-adjacent-duplicates-in-string-ii.py
1 parent ccf3354 commit 82a2573

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def removeDuplicates(self, s, k):
6+
"""
7+
:type s: str
8+
:type k: int
9+
:rtype: str
10+
"""
11+
stk = [['^', 0]]
12+
for c in s:
13+
if stk[-1][0] == c:
14+
stk[-1][1] += 1
15+
if stk[-1][1] == k:
16+
stk.pop()
17+
else:
18+
stk.append([c, 1])
19+
return "".join(c*k for c, k in stk)

0 commit comments

Comments
 (0)