1209. Remove All Adjacent Duplicates in String II - cocoder39/coco39_LC GitHub Wiki

1209. Remove All Adjacent Duplicates in String II

class Solution:
    def removeDuplicates(self, s: str, k: int) -> str:
        stack = []
        for ch in s:
            if stack and stack[-1][0] == ch:
                stack[-1][1] += 1
                if stack[-1][1] == k:
                    stack.pop()   
            else:
                stack.append([ch, 1])
        
        return ''.join([ch * cnt for ch, cnt in stack])