Ctrl K

Digits as Backspace

Easy
A keyboard's Backspace key is broken, so the user presses any digit instead. You are given typed, everything they entered: lowercase letters mixed with digits. Each digit erases the nearest not-yet-erased letter to its left and does not itself appear in the text. Digits are processed from left to right. Return the resulting text. It is guaranteed that every digit has a letter to erase.
Example 1
typed="cat7s" → 'cas'

The 7 erases the t, leaving "cas".

Example 2
typed="ab12cd" → 'cd'

1 erases b, then 2 erases a; "cd" remains.

Example 3
typed="note" → 'note'

No digits, so the string stays as it is.

Constraints
  • 1 ≤ len(typed) ≤ 10⁵
  • typed consists of lowercase Latin letters and digits
  • Every digit has an unerased letter somewhere to its left
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSame Text After Erasing EasyCollapse Letter Pairs EasyErase the Pattern Until It Is Gone MediumCrashes on One Lane MediumStar as Backspace Medium
loading Python…
Input
typed="cat7s"
Expected
'cas'

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