Loading problem...
You are given a string s composed exclusively of lowercase English letters. Your task is to repeatedly identify and remove any pair of adjacent identical characters from the string. This removal process continues iteratively until no more such pairs exist anywhere in the string.
A twin elimination operation consists of finding two consecutive characters that are exactly the same and removing both of them from the string. After each elimination, the characters that were formerly separated by the removed pair now become adjacent, potentially creating new pairs that must also be eliminated.
Continue performing twin eliminations on the string until it reaches a stable state where no adjacent identical pairs remain. Return this final stable string.
The process is deterministic: regardless of which valid pair you choose to remove first, the final result will always be the same. This property ensures a unique answer exists for every input.
Consider the string "abbaca":
abbaca → We see bb is a twin pairaaca → Now aa becomes a twin pairca → No more adjacent twins exist"ca"s = "abbaca""ca"We first remove the "bb" pair from "abbaca", resulting in "aaca". Then we remove the newly adjacent "aa" pair, leaving us with "ca". At this point, no adjacent identical characters remain, so "ca" is our final answer.
s = "azxxzy""ay"We identify "xx" as a twin pair and remove it from "azxxzy", producing "azzy". Now "zz" becomes adjacent, so we remove that pair, yielding "ay". No more twins exist, so "ay" is the result.
s = "aabb"""Starting with "aabb", we can remove "aa" to get "bb", then remove "bb" to get an empty string. Alternatively, removing "bb" first gives "aa", then removing "aa" also yields an empty string. Either way, the final result is an empty string.
Constraints