Remove All Adjacent Duplicates In String
Problem Statement
You are given a string s of lowercase letters.
One move is: pick two equal letters that sit next to each other and delete both. Keep making moves until no two neighbours are equal.
Return the final string. It can be shown that the result is the same no matter which pairs you delete first.
A string s of lowercase English letters.
The string left after all possible deletions (it may be empty).
Delete "bb" to get "aaca". Now the two a's are neighbours, so delete "aa" to get "ca". No equal neighbours are left.
Delete "xx" → "azzy", then delete "zz" → "ay".
- • 1 ≤ s.length ≤ 10^5
- • s contains only lowercase English letters
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Find the first pair of equal neighbours, delete it, rescan from the start
push_back each letter; if it equals back(), pop_back instead
Do exactly what the statement says. Scan for the first place where s[i] == s[i+1], erase those two letters, and scan again from the beginning. Stop when a full scan finds no pair.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Stack translate into production architectures and low-level memory mechanics.