#2030
Smallest K-Length Subsequence With Occurrences of a Letter
master · 1710 · lc hard +32 · verified · 39.7% accepted · 512 likes · top 19%
Description
Given a string s, integers k and repetition, and a character letter, find the lexicographically smallest subsequence of s that has length exactly k and contains letter at least repetition times. It is guaranteed that letter appears at least repetition times in s.
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.
Code
1
2
3