Paths Subjects Questions Quizzes Pricing Search
Beginner Open Pro

Repeated Removal of Adjacent Duplicates

You are given a string s consisting of lowercase letters. In one operation, you may choose two adjacent, equal characters and remove both of them. Apply this operation repeatedly — including to characters that become adjacent as a result of earlier removals — until no two adjacent characters are equal.

Return the final string.

Example 1

Input: s = "abbaca"
Output: "ca"
Explanation: "abbaca" -> remove "bb" -> "aaca" -> remove "aa" -> "ca"

Example 2

Input: s = "azxxzy"
Output: "ay"
Explanation: "azxxzy" -> remove "xx" -> "azzy" -> remove "zz" -> "ay"

Constraints

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

Share this question

← Back to Stacks practice

We use cookies for product analytics to improve OmniAtlas. See our Privacy Policy.