Remove Duplicate Letters
1 approach, code in every language
Problem description
You are given a string `s`. Remove duplicate letters so that every letter appears exactly once in the result.
Among all valid results, return the one that is the smallest in lexicographic order. The relative order of the letters you keep must match their order in the original string.
Example 1
Input: s = "bcabc"
Output: "abc"
Each of a, b, and c appears once, and abc is the lexicographically smallest arrangement achievable while preserving order.
Example 2
Input: s = "cbacdcbc"
Output: "acdb"
Dropping the earlier b and c in favor of later occurrences yields acdb, which is smaller than any other valid single-copy ordering.
Constraints
1 <= s.length <= 10^4s consists of lowercase English letters.
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Smallest Distinct Label is our own problem on the technique behind Remove Duplicate Letters: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free