# Remove All Adjacent Duplicates in String II (Leetcode 1209)

Problem Link: <https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/>

```python
class Solution:
    def removeDuplicates(self, s: str, k: int) -> str:
        
        stack=[]
        
        for i in s:
            if(len(stack)==0):
                stack.append([i,1])
            else:
                if(stack[-1][0]!=i):
                    stack.append([i,1])
                else:
                    if(stack[-1][1]+1==k):
                        stack.pop()
                    else:
                        stack[-1][1]+=1
        
        ans=""
        for i in stack:
            ans=ans+i[0]*i[1]
        return ans
```
