Given a string s, you need to construct the lexicographically smallest subsequence that meets specific constraints. This is like finding the "best" characters from the string while maintaining their original order!
Your task: Extract exactly k characters from string s to form a subsequence that:
- Has length exactly
k - Contains the specified
letterat leastrepetitiontimes - Is the lexicographically smallest among all valid subsequences
Remember: A subsequence maintains the relative order of characters from the original string, but you can skip any characters you don't need.
Example: From "leetcode", if you need 4 characters with 'e' appearing at least 2 times, the answer would be "ecde" (not "eete" because 'c' < 't' lexicographically).
Input & Output
Visualization
Time & Space Complexity
Single pass through the string, each character pushed and popped at most once
Stack stores at most k characters for the result subsequence
Constraints
- 1 โค s.length โค 5 ร 104
- 1 โค k โค s.length
- s consists of lowercase English letters
- letter is a lowercase English letter
- 1 โค repetition โค k
- The letter appears in s at least repetition times