Ctrl K

Crush Letter Runs

Medium
The string text behaves like a match-three game: whenever exactly run equal letters stand side by side, that block vanishes and the left and right parts of the string join up. Joining may create new blocks, and they vanish too. Keep going until nothing more can be removed and return what is left. The result does not depend on the order of removals.
Example 1
text="xyyyxxz", run=3 → 'z'

"yyy" collapses into "xxxz", then "xxx" collapses and "z" is left.

Example 2
text="aabba", run=2 → 'a'

Remove "aa" and "bb", leaving "a".

Example 3
text="abab", run=2 → 'abab'

No two equal letters stand together, so the string stays the same.

Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • 2 ≤ run ≤ 10⁴
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTidy a Folder Route MediumFour-Operation Calculator MediumEvery Letter Once MediumChecking a Tree Record With Hash Marks MediumLongest Path to a File Medium
loading Python…
Input
text="xyyyxxz", run=3
Expected
'z'

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