Skip to content
RED SAGSkills
easy15 ptsstackstrings

Cancel Adjacent Twins

In a tile-matching game, whenever two identical letters sit next to each other they vanish and the pieces on either side close the gap, possibly creating new adjacent twins.

Given the starting row s, return the row after no more twins can vanish. The final result does not depend on the order of removals.

Example 1

Input: s = "abbaca"
Output: "ca"
Explanation: "bb" vanishes giving "aaca", then "aa" vanishes giving "ca".

Example 2

Input: s = "azxxzy"
Output: "ay"

Constraints

  • 0 <= s.length <= 10^5
  • s contains lowercase letters.
Hint 1

Process letters left to right and keep the surviving ones on a stack.

Hint 2

A new letter that matches the top of the stack cancels it.

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Automation Test Engineer, Game Developer

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.