Smallest K-Length Subsequence With Occurrences of a Letter - Problem

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 letter at least repetition times
  • 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

example_1.py โ€” Basic case
$ Input: s = "leet", k = 3, letter = "e", repetition = 1
โ€บ Output: "eet"
๐Ÿ’ก Note: We need 3 characters with at least 1 'e'. The subsequences could be "lee", "let", "eet", "elt", "elt". Among these, "eet" is lexicographically smallest.
example_2.py โ€” Multiple constraints
$ Input: s = "leetcode", k = 4, letter = "e", repetition = 2
โ€บ Output: "ecde"
๐Ÿ’ก Note: We need 4 characters with at least 2 'e's. We start building "ee" but when we see 'c', we can optimize to "ec" then add 'd' and another 'e' to get "ecde" which is optimal.
example_3.py โ€” Edge case with high repetition
$ Input: s = "aaabbbccceeee", k = 6, letter = "e", repetition = 4
โ€บ Output: "abceee"
๐Ÿ’ก Note: We need exactly 6 characters with at least 4 'e's. Since we must have 4 'e's, we can only choose 2 other characters. The smallest available are 'a', 'b', and 'c', so we pick 'a' and 'b' giving us "abceee".

Visualization

Tap to expand
Talent Scout Analogy: Building the Best TeamAvailable Candidates:LEโ˜…Eโ˜…TCODEโ˜…Current Team (Stack):Team RosterEโ˜… (VIP)CDEโ˜… (VIP)Team Size: 4/4 โœ“VIP Count: 2/2 โœ“Selection Process:1. ๐ŸŽฏ Scout each candidate in order2. ๐Ÿ”„ If new candidate better than team member โ†’ replace3. โญ Always ensure VIP quota can be met with remaining pool4. ๐Ÿ“Š Maintain team size = k constraint5. ๐Ÿ† Result: Optimal team "ECDE"Legend:VIP Player (Required Letter)Good Player (Selected)Weak Player (Not Selected)๐ŸŽฏ Key Insight: Use monotonic stack to maintain lexicographical order while satisfying constraints!
Understanding the Visualization
1
Count Resources
First count how many VIP players are available in total
2
Greedy Selection
For each candidate, decide if they improve your current team
3
Smart Replacement
If a better candidate arrives, remove weaker ones from the end - but ensure VIP quota is still achievable
4
Constraint Checking
Always verify you can meet both team size and VIP requirements with remaining candidates
5
Final Team
Your stack contains the optimal team meeting all constraints
Key Takeaway
๐ŸŽฏ Key Insight: The monotonic stack approach ensures we always make locally optimal decisions (keeping smaller characters) while maintaining global constraints (required letter count). This greedy strategy works because we can always verify if future requirements can still be met.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string, each character pushed and popped at most once

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Stack stores at most k characters for the result subsequence

n
2n
โœ“ Linear Space

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
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22 Apple 18
23.3K Views
Medium-High Frequency
~25 min Avg. Time
987 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen