Remove Adjacent Duplicates in String
Given a string s, repeatedly remove a pair of equal adjacent characters until no such pair remains. Return the final string after all removals.
Each removal deletes exactly two matching neighbors. A run of three equal characters like "bbb" loses one pair and keeps the single leftover character, because that leftover has no neighbor of its own to cancel with.
When you remove a pair, the characters on both sides may become adjacent and form a new pair that also gets removed.
Input
s: a string containing lowercase English letters
Output
A string with all adjacent duplicates removed
Examples
Example 1:
Input: s = "abbaca"
Output: "ca"
Explanation:
- Remove "bb": "abbaca" becomes "aaca"
- Remove "aa": "aaca" becomes "ca"
- No more adjacent duplicates, return "ca"
Example 2:
Input: s = "azxxzy"
Output: "ay"
Explanation:
- Remove "xx": "azxxzy" becomes "azzy"
- Remove "zz": "azzy" becomes "ay"
- No more adjacent duplicates, return "ay"
Example 3:
Input: s = "abbbay"
Output: "abay"
Explanation:
- Remove one "bb" pair from the run "bbb": "abbbay" becomes "abay"
- A single "b" is left over with no equal neighbor, so it stays
- No more adjacent pairs, return "abay"
Example 4:
Input: s = "abcd"
Output: "abcd"
Explanation: No adjacent duplicate characters exist.