Loading...
Loading...
In a tile game, two neighbouring tiles with the same letter vanish together, and the tiles on both sides then become neighbours. This repeats until no two neighbours are equal. Read the row of tiles and print what is left, or Empty if nothing is left. The final row is always the same, whatever order the pairs vanish in.
Empty.Example 1
Input
abbaca
Output
ca
bb vanishes, which makes aa neighbours, and they vanish too.
Example 2
Input
abba
Output
Empty
Try the problem first. Open one hint at a time.
Repeating "find a pair and remove it" over and over is slow for 10^5 tiles.
Build the answer in a StringBuilder from left to right. Its last character is the tile that the next tile will touch.
If the next tile equals that last character, delete it with deleteCharAt; otherwise append the tile.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor