Skip to content

Remove All Adjacent Duplicates In String

EasyStack
Decode it first. Clues, constraints, pattern and plan before any code.

You get a string s of lowercase letters. A move picks two adjacent, equal letters and deletes both. Keep making moves until no more are possible, then return the final string. The final string is always the same, whatever order you make the moves in.

s = "abbaca"  →  "ca"    (remove "bb" to get "aaca", then "aa" to get "ca")
s = "azxxzy"  →  "ay"

Constraints

  • 1 <= s.length <= 10^5
  • s has only lowercase English letters.