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.

Example 1
Inputs = "leet", k = 3, letter = "e", repetition = 1
Output"eet"
Among all length-3 subsequences of s containing at least one e, eet is lexicographically smallest.
Example 2
Inputs = "leetcode", k = 4, letter = "e", repetition = 2
Output"ecde"
ecde is the lexicographically smallest subsequence of length 4 that has the letter e appear at least 2 times.

Constraints

  • 1 <= repetition <= k <= s.length <= 5 * 10^4
  • s consists of lowercase English letters.
  • letter is a lowercase English letter, and appears in s at least repetition times.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate