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

You are given a string s, an integer k, a letter letter, and an integer repetition.

Return the lexicographically smallest subsequence of s of length k that has the letter letter appear at least repetition times. The test cases are generated so that the letter appears in s at least repetition times.

A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

A string a is lexicographically smaller than a string b if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b.

Input & Output

Example 1 — Basic Case
$ Input: s = "leetcode", k = 4, letter = "e", repetition = 2
Output: "eetc"
💡 Note: We need a subsequence of length 4 with at least 2 'e's. "eetc" is lexicographically smallest among valid options like "eetc", "eeto", "eeco", etc.
Example 2 — Minimum Repetition
$ Input: s = "leetcode", k = 4, letter = "e", repetition = 1
Output: "ecod"
💡 Note: With only 1 'e' required, we can pick the lexicographically smallest: 'e' from position 1, then 'c', 'o', 'd'.
Example 3 — All Required Letters
$ Input: s = "aaab", k = 3, letter = "a", repetition = 3
Output: "aaa"
💡 Note: We need exactly 3 'a's in our subsequence of length 3, so we take all three 'a's.

Constraints

  • 1 ≤ s.length ≤ 5 × 104
  • 1 ≤ k ≤ s.length
  • 1 ≤ repetition ≤ k
  • letter is a lowercase English letter
  • s contains at least repetition occurrences of letter

Visualization

Tap to expand
Smallest K-Length Subsequence With Occurrences INPUT String s = "leetcode" l e e t c o d e 0 1 2 3 4 5 6 7 Pink boxes = letter 'e' Parameters k = 4 (result length) letter = "e" (required char) repetition = 2 (min count) Total 'e' in string: 3 Need at least: 2 Use Monotonic Stack Stack (greedy) ALGORITHM STEPS 1 Initialize Count remaining 'e's = 3 Stack = [], needed = 2 2 Process Each Char Pop larger chars if safe Keep enough 'e's remaining 3 Greedy Selection Push current if space Ensure k chars + 2 'e's 4 Build Result Take first k chars from stack Stack Trace 'l': push [l] 'e': pop l, push e [e] 'e': push e [e,e] 't': push t [e,e,t] 'c': pop t, push c [e,e,t,c] FINAL RESULT Lexicographically Smallest Subsequence: e e t c Output: "eetc" Verification Length = 4 OK Count of 'e' = 2 OK Lex smallest? OK Why "eetc"? "eetc" < "eeto" < "leto" 'e' is smallest starting char Key Insight: Use a monotonic stack to greedily build the smallest subsequence. Pop larger characters only when: 1) Enough characters remain to fill k slots, and 2) We can still meet the repetition requirement. Track remaining 'letter' count to ensure we never pop too many required characters. TutorialsPoint - Smallest K-Length Subsequence With Occurrences of a Letter | Greedy with Monotonic Stack Time: O(n) | Space: O(k) n = length of string s
Asked in
Google 15 Amazon 12 Facebook 8
18.5K Views
Medium Frequency
~25 min Avg. Time
456 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