Remove All Adjacent Duplicates in String II - Problem
Remove All Adjacent Duplicates in String II

Imagine you're playing a string manipulation game where you need to eliminate groups of k consecutive identical characters. You are given a string s and an integer k. Your task is to repeatedly remove any k adjacent and equal letters from the string until no more removals are possible.

Each removal operation:
• Chooses k adjacent identical characters
• Removes them from the string
• Concatenates the remaining parts
• Continues until no more k-duplicates exist

Example: With s = "deeedbbcccbdaa" and k = 3:
• Remove "eee" → "deedbbcccbdaa" becomes "ddbbcccbdaa"
• Remove "ccc" → "ddbbcccbdaa" becomes "ddbbdaa"
• Remove "dd" (wait, we need k=3) → No more removals possible
• Final result: "ddbbdaa"

The answer is guaranteed to be unique!

Input & Output

example_1.py — Basic Case
$ Input: s = "abcd", k = 2
Output: "abcd"
💡 Note: No two adjacent characters are the same, so no removals are possible. The string remains unchanged.
example_2.py — Single Removal
$ Input: s = "deeedbbcccbdaa", k = 3
Output: "aa"
💡 Note: First remove 'eee' → 'ddbbcccbdaa', then remove 'ccc' → 'ddbbdaa', then remove 'dd' (wait, k=3, so can't remove 'dd'). Actually: remove 'eee' → 'ddbbcccbdaa', remove 'ccc' → 'ddbbdaa'. No more groups of 3 consecutive characters exist. Final result is 'ddbbdaa'. Wait, let me recalculate: 'deeedbbcccbdaa' → remove 'eee' → 'ddbbcccbdaa' → remove 'ccc' → 'ddbbdaa' → remove 'ddd'? No, we have 'dd', 'bb', which are length 2, not 3. So result should be 'ddbbdaa'. Actually, looking more carefully at the string reconstruction after removing 'eee': 'd' + 'edbbcccbdaa' = 'dedbbcccbdaa'. Let me be more careful: 'de[eee]dbbcccbdaa' → 'dedbbcccbdaa' → 'debb[ccc]bdaa' → 'debbdaa'. Hmm, let me trace this with the stack approach to get the correct answer.
example_3.py — Cascading Removals
$ Input: s = "pbbcggttciiippddddssssssss", k = 2
Output: "ps"
💡 Note: Multiple removals cascade: remove 'bb' → 'pcggttciiippddddssssssss', remove 'gg' → 'pcttciiippddddssssssss', remove 'tt' → 'pcciiippddddssssssss', remove 'cc' → 'piiippddddssssssss', remove 'ii' → 'pippddddssssssss', remove 'pp' → 'piddddssssssss', remove 'dd' → 'piddssssssss', remove 'dd' → 'pissssssss', remove 'ss' → 'pssssss', continue removing pairs of 's' until we get 'ps'.

Visualization

Tap to expand
Stack-Based K-Duplicate Removal VisualizationProcessing Timeline →Example: s = "abbbaaca", k = 3Step 1-2: Process 'a', 'b'a:1b:1Stack growsStep 3-4: Process 'b', 'b'a:1b:3Count = k!Remove!Step 5-6: Process 'a', 'a'a:3Count = k!Remove!Step 7-8: Process 'c', 'a'c:1a:1Final!Final Result"ca"
Understanding the Visualization
1
Initialize Stack
Start with empty stack to track (character, count) pairs
2
Process Each Character
Add to existing group or start new group
3
Remove K-Duplicates
When count reaches k, instantly remove the group
4
Handle Cascading
Removal may create new k-duplicates at junction points
Key Takeaway
🎯 Key Insight: The stack naturally handles cascading removals - when we remove k characters, the stack automatically exposes what remains, potentially creating new k-duplicates that are immediately detectable.

Time & Space Complexity

Time Complexity
⏱️
O(n)

We process each character exactly once. Even when removals happen, each character is pushed and popped at most once from the stack.

n
2n
Linear Growth
Space Complexity
O(n)

In the worst case (no removals possible), the stack will contain all characters. The output string also requires O(n) space.

n
2n
Linearithmic Space

Constraints

  • 1 ≤ s.length ≤ 105
  • 2 ≤ k ≤ 104
  • s only contains lowercase English letters
Asked in
Facebook 45 Amazon 38 Google 32 Microsoft 28
128.4K Views
Medium-High Frequency
~18 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen