Smallest K-Length Subsequence With Occurrences of a Letter — LeetCode 2030 Python Solution

HardStackGreedyStringMonotonic Stack
Problem
#2030
Pattern
Stack
Reading time
2 min

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

Example

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:

Complexity

MeasureComplexity
TimeO(n log n)
SpaceO(1) to O(n) auxiliary

Pattern: Stack

When the most recent unresolved thing is the one that matters, use a stack. LeetCode 2030. Smallest K-Length Subsequence With Occurrences of a Letter is filed here because LeetCode tags it Stack, which is the vocabulary this hub collects.

The stack guide has the Python template for the pattern and the 194 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2030. Smallest K-Length Subsequence With Occurrences of a Letter?
LeetCode 2030. Smallest K-Length Subsequence With Occurrences of a Letter is rated Hard on LeetCode.
What topics does LeetCode 2030. Smallest K-Length Subsequence With Occurrences of a Letter cover?
LeetCode 2030. Smallest K-Length Subsequence With Occurrences of a Letter is tagged Stack, Greedy, String and Monotonic Stack on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview