Lexicographically Smallest String After Deleting Duplicate Characters

You are given a string s that consists of lowercase English letters.

You can perform the following operation any number of times, possibly zero times:

  • Choose any letter that appears at least twice in the current string s and delete any one occurrence.

Return the lexicographically smallest resulting string that can be formed this way.

Example 1
Inputs = "aaccb"
Output"aacb"
We can form the strings "acb", "aacb", "accb", and "aaccb"; "aacb" is the lexicographically smallest one.
Example 2
Inputs = "z"
Output"z"
We cannot perform any operations, so the only string we can form is "z".

Constraints

  • 1 <= s.length <= 10^5
  • s contains lowercase English letters only.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate