Remove Duplicate Letters
Given a string s, remove duplicate letters so that every letter appears exactly once, and return the smallest result in lexicographical order among all such possible strings.
Open official problem prompt ↗Produce the alphabetically smallest string that still contains every distinct letter of the input exactly once, keeping original order.
You are packing one of each souvenir type into a line on a shelf. If a nicer arrangement is possible because you will pass another copy of a bulky item later, you set the bulky one back now and grab it again when it fits better.
- Input
- s = "cbacdcbc"
- Output
- "acdb"
- Why
- Every distinct letter (a, b, c, d) appears once, and 'acdb' is lexicographically smaller than any other one-of-each arrangement obtainable by deletion.
1 <= s.length <= 10^4s consists of lowercase English letters