A row of tiles is given as a string of lowercase English letters. Whenever two adjacent tiles show the same letter, that pair is removed together, and the tiles on either side become adjacent (and may then form a new removable pair). Keep removing adjacent equal pairs until no two adjacent tiles are equal. The final string is unique regardless of the order in which pairs are removed.
Print the final string. If every tile is removed, print an empty line.
Line 1: a string of lowercase English letters.
A single line: the final string after all adjacent equal pairs have been removed (an empty line if nothing remains).
Example 1
Input
abbaca
Expected
ca
Explanation
The two b's cancel giving 'aaca', then the two a's cancel giving 'ca'. No adjacent pair remains, so the answer is 'ca'.
Example 2
Input
xyzzyx
Expected
(empty)Explanation
The z's cancel to give 'xyyx', then the y's cancel to give 'xx', then the x's cancel, leaving nothing. The output is an empty line.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →