Skip to main content
Back to problems
Leetcode
Medium
Strings
Stacks
Remove All Adjacent Duplicates In String

Repeatedly remove adjacent equal characters from a string until no such pair remains.

Acceptance 0%
Problem Statement

Given a string s, repeatedly delete any pair of neighboring characters that are the same. After each deletion, the string closes up and may create new adjacent duplicates, so the process continues until no two adjacent characters are equal.

Return the final string after all possible removals have been performed.

Input Format

  • A single string s consisting of lowercase English letters.

Output Format

  • Return the reduced string after removing all adjacent duplicate pairs until stable.

Constraints

  • 1 <= s.length <= $10^{5}$
  • s contains only lowercase English letters
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "abbaca"

Output

"ca"

Explanation

Remove "bb" -> "aaca". Then remove "aa" -> "ca". No more adjacent duplicates remain.

Example 2

Input

s = "azxxzy"

Output

"ay"

Explanation

Remove "xx" -> "azzy". Then remove "zz" -> "ay".

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.