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

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`.


Python solution:
import math
import itertools
import bisect
import collections
import string
import heapq
import functools
import sortedcontainers
from typing import List, Dict, Tuple, Iterator

class Solution:
  def smallestSubsequence(self, s: str, k: int, letter: str, repetition: int) -> str:
    stack = []
    required = repetition
    nLetters = s.count(letter)

    for i, c in enumerate(s):
      while stack and stack[-1] > c  and len(stack) + len(s) - i - 1 >= k and (stack[-1] != letter or nLetters > required):
        if stack.pop() == letter:
          required += 1
      if len(stack) < k:
        if c == letter:
          stack.append(c)
          required -= 1
        elif k - len(stack) > required:
          stack.append(c)
      if c == letter:
        nLetters -= 1

    return ''.join(stack)


Java solution:
class Solution {
  public String smallestSubsequence(String s, int k, char letter, int repetition) {
    StringBuilder sb = new StringBuilder();
    Deque<Character> stack = new ArrayDeque<>();
    int required = repetition;
    int nLetters = (int) s.chars().filter(c -> c == letter).count();

    for (int i = 0; i < s.length(); ++i) {
      final char c = s.charAt(i);
      while (!stack.isEmpty() && stack.peek() > c && stack.size() + s.length() - i - 1 >= k &&
             (stack.peek() != letter || nLetters > required))
        if (stack.pop() == letter)
          ++required;
      if (stack.size() < k)
        if (c == letter) {
          stack.push(c);
          --required;
        } else if (k - stack.size() > required) {
          stack.push(c);
        }
      if (c == letter)
        --nLetters;
    }

    for (final char c : stack)
      sb.append(c);

    return sb.reverse().toString();
  }
}


C++ solution:
class Solution {
 public:
  string smallestSubsequence(string s, int k, char letter, int repetition) {
    string ans;
    vector<char> stack;
    int required = repetition;
    int nLetters = ranges::count(s, letter);

    for (int i = 0; i < s.length(); ++i) {
      const char c = s[i];
      while (!stack.empty() && stack.back() > c &&
             stack.size() + s.length() - i - 1 >= k &&
             (stack.back() != letter || nLetters > required)) {
        const char popped = stack.back();
        stack.pop_back();
        if (popped == letter)
          ++required;
      }
      if (stack.size() < k)
        if (c == letter) {
          stack.push_back(c);
          --required;
        } else if (k > stack.size() + required) {
          stack.push_back(c);
        }
      if (c == letter)
        --nLetters;
    }

    for (const char c : stack)
      ans += c;

    return ans;
  }
};