Problem · Stack
Remove K Digits
Learn this problemProblem statement
You are given a string num that represents a non-negative integer, and an integer k.
Remove exactly k digits from num so the remaining digits stay in their original relative order and form the smallest possible integer.
Return that integer as a string. Do not keep leading zeros, except for the integer 0 itself.
Function
removeKdigits(num: String, k: int) → StringExamples
Example 1
num = "1432219"k = 3return = "1219"Removing the digits 4, 3, and 2 from 1432219 leaves 1219, which is the smallest remaining integer.
Example 2
num = "10200"k = 1return = "200"Removing the leading 1 leaves 0200, which becomes 200 after leading zeros are stripped.
Example 3
num = "10"k = 2return = "0"Every digit is removed, so the result is 0.
Constraints
1 <= num.length <= 10^5.1 <= k <= num.length.numconsists of digits only.numhas no leading zeros except whennumis"0".