Smallest Subsequence of Distinct Characters - Problem
Imagine you have a string of characters and you need to pick the lexicographically smallest subsequence that contains every distinct character exactly once. This is like creating the most optimal representative sample from your string!
Given a string s, your task is to return the lexicographically smallest subsequence of s that contains all the distinct characters of s exactly once.
What makes this challenging?
- You must maintain the relative order of characters from the original string (subsequence property)
- You want the lexicographically smallest result possible
- Each distinct character must appear exactly once
Example: For string "cbacdcbc", the answer is "acdb" because it's the smallest possible subsequence containing all distinct characters {a, b, c, d} exactly once.
Input & Output
example_1.py โ Basic Case
$
Input:
s = "bcabc"
โบ
Output:
"abc"
๐ก Note:
The distinct characters are 'a', 'b', and 'c'. The lexicographically smallest subsequence containing all of them exactly once is "abc".
example_2.py โ Complex Case
$
Input:
s = "cbacdcbc"
โบ
Output:
"acdb"
๐ก Note:
The distinct characters are 'a', 'b', 'c', and 'd'. We need to find the lexicographically smallest subsequence. Starting with 'a', then 'c' (since we need it), then 'd', then 'b' gives us "acdb".
example_3.py โ Single Character
$
Input:
s = "aaa"
โบ
Output:
"a"
๐ก Note:
Only one distinct character 'a', so the result is "a".
Constraints
- 1 โค s.length โค 104
- s consists of lowercase English letters
- Important: The result must be a subsequence (maintain relative order) and contain each distinct character exactly once
Visualization
Tap to expand
Understanding the Visualization
1
Count Resources
Count how many times each character appears - this tells us if we can afford to skip a character now
2
Greedy Selection
For each character, decide whether to include it now or wait for a better position
3
Smart Backtracking
If we find a better character, remove recent worse choices (but only if they can be added back later)
4
Optimal Result
The stack contains our optimal selection maintaining both lexicographic order and the subsequence property
Key Takeaway
๐ฏ Key Insight: The monotonic stack allows us to make locally optimal decisions while maintaining the ability to backtrack when we discover globally better choices, as long as we haven't exhausted our future opportunities for the characters we remove.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code