Ctrl K

Every Letter Once

Medium
You get a string text of lowercase English letters. Strike out some letters so that every letter that occurs in text is left exactly once. The remaining letters keep their order. Among all possible results, return the lexicographically smallest one.
Example 1
text="dbcbad" → 'bcad'

We must keep one each of a, b, c and d. "bcad" can be obtained by striking letters out and is smaller than any other option: "abcd" is impossible because neither b nor c appears after the a.

Example 2
text="zzaz" → 'az'

We can get "za" or "az"; the latter is smaller.

Example 3
text="q" → 'q'
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 problemsErase Digits for the Smallest Number MediumOne Bead of Each Color MediumSmallest Pick With a Required Letter HardSmallest Up-Down Sequence MediumFix the Middle of the Line Medium
loading Python…
Input
text="dbcbad"
Expected
'bcad'

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