Remove All Adjacent Duplicates In String

EasyStringHash TableStack

Description

Given a string s, repeatedly remove adjacent duplicate letters. Continue removing duplicates until no adjacent pair of equal characters remains. Return the result as a string.

Examples

Input:s = "abbaca"
Output:"ca"
Explanation:

Using a stack: 'abbaca' → remove 'bb' → 'aaca' → remove 'aa' → 'ca'. The final result is 'ca'.

Input:s = "abccba"
Output:""
Explanation:

Remove cc to get "abba", then remove bb to get "aa", finally remove aa to get empty string.

Input:s = "aabbccdd"
Output:""
Explanation:

Remove aa to get "bbccdd", then remove bb to get "ccdd", then remove cc to get "dd", finally remove dd to get empty string.

Constraints

  • 1 ≤ s.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!