Skip to content
Onsite

Remove K Digits

MediumP2Coding

Given a string num representing a non-negative integer and an integer k, remove exactly k digits from num so that the remaining number is as small as possible. Return it as a string without leading zeros. If the result is empty, return "0".

Example 1

Input
num = "1432219", k = 3
Output
"1219"

Example 2

Input
num = "10200", k = 1
Output
"200"
Why
Remove the 1; the leading zero is then dropped.

Example 3

Input
num = "10", k = 2
Output
"0"

Constraints

  • 1 <= num.length <= 10^5
  • 0 <= k <= num.length
  • num has only digits and no leading zeros unless it is exactly "0"

Hints

Run checks the 3 visible tests, Submit all 13.