Ctrl K

Letters of the Final Round

Medium
You are given a string text of lowercase English letters. It gets erased in rounds. In one round, for every letter from a to z, the leftmost occurrence of that letter (if any) is deleted from the string. All other characters keep their order. Rounds repeat until the string is empty. Return the string as it looked right before the final round, the one that leaves nothing behind. For example, text = "banana" goes "banana" → "ana" → "a" → "", so the answer is "a".
Example 1
text='banana' → 'a'

Round 1 removes the first "b", "a" and "n", leaving "ana". Round 2 removes the first "a" and "n", leaving "a". Round 3 removes it and the string is empty, so the answer is "a".

Example 2
text='xyzzyx' → 'zyx'

Round 1 removes the first "x", "y" and "z", leaving "zyx". Round 2 removes everything.

Example 3
text='cab' → 'cab'
Constraints
  • 1 ≤ len(text) ≤ 5·10⁵
  • text consists only of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMore Than a Third of the Votes MediumMost Common Values MediumJobs With a Cooldown MediumMost Searched Words MediumTriples Hitting the Target Medium
loading Python…
Input
text='banana'
Expected
'a'

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