> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Remove All Adjacent Duplicates in String II

> Tested Python solution for LeetCode 1209 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1209, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Stack](/catalog/topics/stack). [View on LeetCode](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1209   # by problem number
lcpy gen -s remove_all_adjacent_duplicates_in_string_ii   # by problem name
```

## Problem

You are given a string `s` and an integer `k`, a `k` **duplicate removal** consists of choosing `k` adjacent and equal letters from `s` and removing them, causing the left and the right side of the deleted substring to concatenate together.

We repeatedly make `k` **duplicate removals** on `s` until we no longer can.

Return *the final string after all such duplicate removals have been made*. It is guaranteed that the answer is **unique**.

### Examples

```
Input: s = "abcd", k = 2
Output: "abcd"
Explanation: There's nothing to delete.
```

```
Input: s = "deeedbbcccbdaa", k = 3
Output: "aa"
Explanation: First delete "eee" and "ccc", get "ddbbbdaa"
Then delete "bbb", get "dddaa"
Finally delete "ddd", get "aa"
```

```
Input: s = "pbbcggttciiippooaais", k = 2
Output: "ps"
```

### Constraints

* `1 <= s.length <= 10^5`
* `2 <= k <= 10^4`
* `s` only contains lowercase English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/remove_all_adjacent_duplicates_in_string_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/remove_all_adjacent_duplicates_in_string_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(len(s))
    # Space: O(len(s))
    def remove_duplicates(self, s: str, k: int) -> str:
        stack: list[tuple[str, int]] = []
        for char in s:
            if stack and stack[-1][0] == char:
                char, count = stack[-1]
                if count + 1 == k:
                    stack.pop()
                else:
                    stack[-1] = (char, count + 1)
            else:
                stack.append((char, 1))
        return "".join(char * count for char, count in stack)
```

## Complexity

| Time | Space |
| - | - |
| O(len(s)) | O(len(s)) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.