Remove All Adjacent Duplicates In String - Problem
You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.
We repeatedly make duplicate removals on s until we no longer can.
Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abbaca"
›
Output:
"ca"
💡 Note:
First remove "bb" to get "aaca", then remove "aa" to get "ca". No more adjacent duplicates exist.
Example 2 — Complete Removal
$
Input:
s = "azxxzy"
›
Output:
"ay"
💡 Note:
Remove "xx" to get "azzy", then remove "zz" to get "ay". Final result is "ay".
Example 3 — No Duplicates
$
Input:
s = "abc"
›
Output:
"abc"
💡 Note:
No adjacent duplicates exist, so the string remains unchanged.
Constraints
- 1 ≤ s.length ≤ 105
- s consists of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
Original string with adjacent duplicates
2
Process
Remove adjacent duplicate pairs repeatedly
3
Output
Final string with no adjacent duplicates
Key Takeaway
🎯 Key Insight: Use a stack to efficiently track characters and automatically handle cascading removals when duplicates are found
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code