Remove Adjacent Duplicates Visualization & Animation

Removes all adjacent duplicate characters repeatedly using a stack; O(n) time and space.

## What is it? Remove all adjacent duplicate characters from a string repeatedly until no adjacent duplicates remain. This is a classic stack-based string cleaning problem. ## How it works **Stack approach:** - Iterate through each character in the string - If the stack is not empty and the top of the stack equals the current character → pop (remove the pair) - Otherwise → push the current character - At the end, the stack contains the result string ## When to use - String simplification and cleaning - Undo-style problems where matching pairs cancel out - As a building block for more complex stack problems ## Key Points - O(n) time — each character is pushed and popped at most once - O(n) space — stack can hold up to n characters - The result is the characters left in the stack after processing - Extension: "Remove k adjacent duplicates" uses a stack of (char, count) pairs

Category: algorithms

Difficulty: beginner

Time Complexity: O(n)

Space Complexity: O(n)

Remove Adjacent Duplicates

beginner

Removes all adjacent duplicate characters repeatedly using a stack; O(n) time and space.

Cancels0
Init
empty
Char Stack
i
a
[0]
b
[1]
b
[2]
a
[3]
c
[4]
a
[5]
s="abbaca". Push chars onto a stack — if incoming char matches the top, they cancel each other out.