Back to course home
0% completed
Vote For New Content
Remove Duplicate Letters (medium)
Problem Statement
Given a string s
, remove all duplicate letters from the input string while maintaining the original order of the letters.
Additionally, the returned string should be the smallest in lexicographical order among all possible results.
A string is in the smallest lexicographical order if it appears first in a dictionary. For example, "abc"
is smaller than "acb"
because "abc"
comes first alphabetically.
Examples:
-
- Input: "babac"
- Expected Output: "abc"
- Justification:
- After removing 1
b
and 1a
from the input string, we can getbac
, andabc
strings. The final answer is 'abc', which is the smallest lexicographical string without duplicate letters.
- After removing 1
-
- Input: "zabccde"
- Expected Output: "zabcde"
- Justification: Removing one of the 'c's forms 'zabcde', the smallest string in lexicographical order without duplicates.
-
- Input: "mnopmn"
- Expected Output: "mnop"
- Justification: Removing the second 'm' and 'n' gives 'mnop', which is the smallest possible string without duplicate characters.
Constraints:
- 1 <= s.length <= 10<sup>4</sup>
- s consists of lowercase English letters.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible