 All Problems
Remove K Digits
medium
stack
greedy
string
google
amazon
facebook

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

Example 1:

Input: 1432219
k: 3
Output: 1219

Example 2:

Input: 10200
k: 1
Output: 200

Example 3:

Input: 10
k: 2
Output: 0

Constraints:

  • 1 ≤ num.length ≤ 10⁵
  • num consists of digits only
  • 0 ≤ k ≤ num.length

Input format: First line: number string. Second line: k.

Output format: Smallest number string.

Run to check your code against the sample cases, or submit to run every case