Remove duplicate characters so every letter appears once and the result is lexicographically smallest among all valid subsequences.
Remove Duplicate Letters
gfgGiven a string s, remove some characters so that each distinct character appears exactly once in the final string. The result must be a subsequence of s and among all valid subsequences it should be the lexicographically smallest possible.
Return that smallest string.
A subsequence keeps the relative order of the remaining characters.
s consisting of lowercase English letters.s is non-empty.s that contains every distinct character from s exactly once.1 <= s.length.s contains only lowercase English letters.s.s must appear exactly once in the answer.Example 1
Input
s = "bcabc"
Output
"abc"
Explanation
The valid subsequences with all distinct letters once include "bca", "bac", and "abc". The smallest lexicographically is "abc".
Example 2
Input
s = "cbacdcbc"
Output
"acdb"
Explanation
We need one of each distinct letter: a, b, c, d. The smallest subsequence satisfying this is "acdb".
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.