Menu
CoddyTech

Remove K Digits

You get a non-negative number written as a string of digits, num, and an integer k. Remove exactly k digits and keep the rest in their original order, so that the number left is as small as possible.

Return it as a string without leading zeros. If nothing is left, or only zeros, return "0".

Function

removeKdigits(num: string, k: integer) → string
numstring
the number, written as a string of digits
kinteger
how many digits to remove
Returnsstring
the smallest number you can leave, as a string without leading zeros

Constraints

  • 1 ≤ num.length ≤ 4 × 104
  • 0 ≤ k ≤ num.length
  • num holds only the digits 0 to 9.
  • num has no leading zeros, except when it is the number zero itself, "0".

Examples

Input
num = "4325043"k = 3
Output
"2043"
Explanation
Dropping the 4 and the first 3 puts the 2 in front. The 0 cannot lead: four digits stand before it and you have three removals. The last removal takes the 5, which sits before a smaller 0.
challenge icon

Follow-up

How would you change the solution to remove k digits and leave the largest number instead?

Reset code
def removeKdigits(num, k):
    # Write code here

Test #1

Test #2

Test #3

Arguments

num = "4325043"
k = 3

Expected Output

"2043"