Skip to content
Decoder

Remove K Digits

Medium

Problem

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".

Example 1

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

Example 2

Input
num = "10200", k = 1
Output
"200"
Why
removing the 1 leaves "0200"

Example 3

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

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.