Skip to content
LC-1209 Medium LeetCode

1209. Remove All Adjacent Duplicates in String II

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 59% Topics: String, Stack
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(n)

class Solution(object):
    def removeDuplicates(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: str
        """
        stk = [['^', 0]]
        for c in s:
            if stk[-1][0] == c:
                stk[-1][1] += 1
                if stk[-1][1] == k:
                    stk.pop()
            else:
                stk.append([c, 1])
        return "".join(c*k for c, k in stk)

Solution from kamyu104/LeetCode-Solutions · MIT