Skip to content

Remove K Digits

MediumStack
Decode it first. Clues, constraints, pattern and plan before any code.

You get a string num that holds a non-negative integer, and an integer k. Remove exactly k digits from num so that the number left is as small as possible, and return it as a string. The result must not have leading zeros; if nothing is left, return "0".

num = "1432219", k = 3  →  "1219"
num = "10200",   k = 1  →  "200"    (removing the 1 leaves "0200")
num = "10",      k = 2  →  "0"

Constraints

  • 1 <= k <= num.length <= 10^5
  • num contains only digits.
  • num has no leading zeros, except for the number zero itself.