As reported: D. E. Shaw MTS onsite round 2 (Oct 2024): given a string and an integer
k, return the lexicographically smallest string obtainable after at mostkdeletions.
Given a string s and an integer k, delete at most k characters from s (the remaining characters keep their order). Return the lexicographically smallest string you can obtain.
A string is lexicographically smaller than another if at the first position where they differ it has the smaller character, or if it is a proper prefix of the other (so "ab" < "abc"). The empty string is the smallest of all.
Example 1
s = "cbad", k = 2
Output: "ad"
Delete c and b.
Example 2
s = "abc", k = 1
Output: "ab"
The string is already increasing, so deleting the last character gives a smaller prefix.
Example 3
s = "bcab", k = 3
Output: "a"
Constraints: 1 ≤ s.length ≤ 10^5; s has lowercase English letters; 0 ≤ k ≤ s.length.
Code runner format: one JSON value per line: s, then k. Implement smallestAfterDeletions(s, k) and return the string.
Clarifications
- Using fewer than
kdeletions is allowed; withk = s.lengththe answer is the empty string. - This is close to LeetCode 402 — Remove K Digits (/remove-k-digits), which removes exactly
kdigits and drops leading zeros; here the characters are letters and up tokmay be removed.