Remove Touching Twins

EasyStack≈ 15 min

In a string text, two equal letters standing side by side are touching twins. Delete a pair of touching twins, and repeat, until no pair is left; deleting one pair can bring two other letters together and make a new pair. Return the text that is left.

The result is the same whichever pair you delete first. Upper and lower case are different letters.

Examples

text
text = "abbaca"   →  "ca"
text = "azxxzy"   →  "ay"
text = "abc"      →  "abc"
text = "aaaa"     →  ""

In the first example, deleting bb leaves "aaca"; now the two as touch, and deleting them leaves "ca".

Constraints

  • Between 0 and 20,000 letters.
  • Aim for O(n): look at each letter a constant number of times.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 8 on the judge, 4 of them hidden, and measures how your solution scales.