Smallest K-Length Subsequence With Occurrences of a Letter Problem
Description
LeetCode Problem 2030.
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.
Example 1:
Input: s = "leet", k = 3, letter = "e", repetition = 1
Output: "eet"
Explanation: There are four subsequences of length 3 that have the letter 'e' appear at least 1 time:
- "lee" (from "leet")
- "let" (from "leet")
- "let" (from "leet")
- "eet" (from "leet")
The lexicographically smallest subsequence among them is "eet".
Example 2:

Input: s = "leetcode", k = 4, letter = "e", repetition = 2
Output: "ecde"
Explanation: "ecde" is the lexicographically smallest subsequence of length 4 that has the letter "e" appear at least 2 times.
Example 3:
Input: s = "bb", k = 2, letter = "b", repetition = 2
Output: "bb"
Explanation: "bb" is the only subsequence of length 2 that has the letter "b" appear at least 2 times.
Constraints:
1 <= repetition <= k <= s.length <= 5 * 10^4sconsists of lowercase English letters.letteris a lowercase English letter, and appears insat leastrepetitiontimes.
Solution
Approach: Monotone Stack
To get the lexicographically smallest subsequence, scan left to right with a stack and pop a larger character whenever a smaller one arrives. Two constraints limit popping, because the result must have exactly k characters and at least repetition copies of letter:
- Pop only if the characters left in the string can still fill the stack back up to
k:stack size + remaining characters >= k. - Never pop a
letterif the copies on the stack plus the copies still ahead would drop belowrepetition.
Track cnt, the copies of letter on the stack, and suf, the copies of letter in the rest of the string. After popping, push the current character if the stack has fewer than k characters. A non-letter character is pushed only if enough room remains for the missing copies of letter (stack size + max(repetition - cnt, 0) < k); a letter is always pushed.
Complexity: O(n) time and O(k) space.
class Solution:
def smallestSubsequence(self, s: str, k: int, letter: str, repetition: int) -> str:
n = len(s)
suf = s.count(letter) # copies of letter in s[i:]
stk = []
cnt = 0 # copies of letter on the stack
for i, c in enumerate(s):
while (
stk
and stk[-1] > c
and len(stk) + n - i - 1 >= k
and (stk[-1] != letter or cnt + suf - 1 >= repetition)
):
if stk.pop() == letter:
cnt -= 1
if len(stk) < k:
if c == letter:
stk.append(c)
cnt += 1
elif len(stk) + max(repetition - cnt, 0) < k:
stk.append(c)
if c == letter:
suf -= 1
return "".join(stk)