Remove All Adjacent Duplicates In String

Problem Repeatedly delete pairs of adjacent equal characters from a string until no such pair remains, then return the final string. Deletions can cascade: removing one pair may bring two new equal characters together.

Input / Output

  • Input: a string s of lowercase letters.
  • Output: the fully reduced string after all possible adjacent-pair deletions.

Constraints

  • |s| up to 10^5; a single O(n) pass is expected (repeatedly rescanning is O(n^2)).

Example

  • "abbaca" → "ca" (remove "bb" → "aaca", then "aa" → "ca").
  • "azxxzy" → "ay".
asked …
LeaderboardSalaryAccount