Repeatedly remove adjacent equal characters from a string until no such pair remains.
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
sconsisting of lowercase English letters.
Output Format
- Return the reduced string after removing all adjacent duplicate pairs until stable.
Constraints
1 <= s.length <= $10^{5}$scontains only lowercase English letters
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.