Ctrl K

Popping Pairs

Easy
In a game, a row of balls lies on a track, each labeled with a lowercase English letter; the row is written as the string text. When two neighbouring balls are the same, they pop and the rest close the gap, which may bring new equal neighbours together. Pairs keep popping until no equal neighbours remain. Return the string of balls that are left. It can be shown that the result does not depend on the order in which pairs pop.
Example 1
text="kayyak" → ''

Remove yy to get kaak, then aa to get kk, then kk leaves an empty string.

Example 2
text="abccbd" → 'ad'

Removing cc brings the two bs together; they go too, leaving ad.

Example 3
text="xyz" → 'xyz'
Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBracket Balance EasySame Text After Erasing EasyPeel the Outer Brackets EasyCase Pairs Cancel Out EasySteps Back to the Top Folder Easy
loading Python…
Input
text="kayyak"
Expected
''

Run checks the first 3 tests in your browser. Submit checks all 43 tests on the server, hidden ones included.