Given a string num representing a non-negative integer and an integer k, return the smallest possible integer after removing exactly k digits from num. The digits that remain keep their original order.
The result should not contain any leading zeros (except for the number 0 itself). If removing the digits leaves nothing, return "0".
Function Signature
def remove_k_digits(num: str, k: int) -> str:
Rules
-
Remove exactly
k
digits. The digits you keep stay in their original relative order.
-
Return the result as a decimal string with leading zeros stripped.
-
Return
"0"
when no digits remain, or when only zeros remain.
Constraints
-
1 <= k <= len(num) <= 10^5
-
num
consists of only digits.
-
num
does not have any leading zeros except for the zero itself.
Examples
Input: num = "1432219", k = 3
Output: "1219"
Remove the digits 4 and 3 and one of the two 2s to get 1219. Every result has four digits, and no choice of removals gives a four-digit result smaller than 1219.
Input: num = "10200", k = 1
Output: "200"
Remove the leading 1 to get "0200", then strip the leading zero to get "200". Removing any other digit leaves a number that starts with 1, which is larger.
Input: num = "10", k = 2
Output: "0"
Both digits are removed, so nothing remains and the answer is "0".