Talentd
FleetCode

3816. Lexicographically Smallest String After Deleting Duplicate Characters

Hard20.9% acceptance2 companies

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:

Input: s = "aaccb"

Output: "aacb"

Explanation:

We can form the strings "acb", "aacb", "accb", and "aaccb". "aacb" is the lexicographically smallest one.

For example, we can obtain "aacb" by choosing 'c' and deleting its first occurrence.

Example 2:

Input: s = "z"

Output: "z"

Explanation:

We cannot perform any operations. The only string we can form is "z".

Constraints:

  • 1 <= s.length <= 105
  • s contains lowercase English letters only.

Asked at 2 companies

Hints open one at a time

Hint 1
Solve greedily.
Hint 2
Each distinct letter must appear at least once in the final string.
Hint 3
For each letter, maintain a deque of its positions.
Hint 4
At each step, try letters from 'a' to 'z' and pick the smallest letter whose earliest position lies within a safe window.
Hint 5
Do not pick an occurrence if choosing it would make some other letter impossible to keep.
Hint 6
Mark positions as used and repeat, always minimizing the next chosen character.
27:49
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def lexSmallestAfterDeletion(self, s: str) -> str:
3
Starter codePython 3Spaces: 4
Examples
Input
"aaccb"
Input
"z"
Your code is saved on this device.Solution