Collapse Adjacent Duplicate Letters

Implement collapseAdjacentDuplicates

Given a lowercase string s, repeatedly remove any two adjacentAdjacentDirectly next to each other in the current string — after a removal, characters that weren't originally adjacent can become adjacent, potentially triggering another removal. identical characters until no such pair remains, and return the final result. Because a removal can expose a brand-new adjacent match — collapsing "mississippi" strips the double 's', which brings the double 'i' together — the naive approach has to keep re-scanning from the top until a full pass changes nothing. A single-pass stack sidesteps the re-scanning entirely: compare each new character only to whatever is currently on top of the stack. A match means that pair just cancelled, so pop instead of pushing — which conveniently exposes exactly the right character to compare against next, letting cascades resolve for free within one linear pass.

Example 1:

Input: s = "mississippi"

Output: "m"

Example 2:

Input: s = "wxxyyz"

Output: "wz"

Example 3:

Input: s = "aabaa"

Output: "b"

+ 6 hidden test cases run on Submit.

Constraints:

  • 1 ≤ s.length ≤ 20
  • s consists only of lowercase English letters

s =

mississippi