Remove K Digits
Medium · stack
You are given num, a string of digits that writes a non-negative whole number, and an integer k. Delete exactly k digits from num. The digits that are left keep their original order.
Return the smallest number you can make this way, as a string. The answer must not have leading zeros: if zeros end up at the front, drop them. If no digits are left, or only zeros are left, return "0".
Examples
Example 1
Input: num = "4132", k = 2
Output: "12"
Deleting the 4 and the 3 leaves 12, and no other choice of two digits gives a smaller number.
Example 2
Input: num = "10200", k = 1
Output: "200"
Deleting the 1 leaves 0200, and the leading zero is dropped.
Example 3
Input: num = "9", k = 1
Output: "0"
Nothing is left, so the answer is 0.
Constraints
- 1 <= len(num) <= 10^5
- 0 <= k <= len(num)
- num contains only the digits 0 to 9.
- num has no leading zeros, except when num is exactly "0".