Remove exactly k digits from a non-negative integer so the remaining number is as small as possible.
Given a non-negative integer represented as a string num and an integer k, remove exactly k digits from the string so that the resulting number is the smallest possible.
Return the smallest possible number as a string. The result should not contain unnecessary leading zeros. If all digits are removed or the number becomes empty after trimming, return "0".
The relative order of the remaining digits must stay the same.
Input Format
- A string
numrepresenting a non-negative integer. - An integer
k, the number of digits to remove.
Output Format
- Return a string representing the smallest possible number after removing exactly
kdigits.
Constraints
1 <= num.length0 <= k <= num.lengthnumcontains only digits0-9- The answer must preserve the relative order of the remaining digits
Example 1
Input
num = "1432219", k = 3
Output
"1219"
Explanation
Remove the digits 4, 3, and 2 to make the smallest possible number while preserving order.
Example 2
Input
num = "10200", k = 1
Output
"200"
Explanation
Removing the leading 1 gives "0200", which is normalized to "200".
Show 1 more example
Example 3
Input
num = "10", k = 2
Output
"0"
Explanation
All digits are removed, so the result is "0".
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.