Remove K Digits

Given a non-negative integer num represented as a string and an integer k, remove exactly k digits from the number so that the new number is the smallest possible. Return the result as a string. Pattern focus: Local Choice Proof. Use a monotonic stack to remove digits when a smaller digit appears later, because keeping a larger digit earlier can never help form a smaller number.

Input Format

num = non-negative integer string, k = number of digits to remove

Output Format

smallest possible number string after removing exactly k digits

Constraints

  • 1 <= num.length <= 10^5
  • num consists of digits only
  • 0 <= k <= num.length

Examples

Example 1:

Input:

num = "1432219"
k = 3

Output:

1219

Explanation:

Removing 4, 3, and 2 produces the smallest possible number.

Example 2:

Input:

num = "10200"
k = 1

Output:

200

Explanation:

Removing the leading 1 yields 0200, which normalizes to 200.

Example 3:

Input:

num = "10"
k = 2

Output:

0

Explanation:

Removing all digits leaves 0 by definition.

Loading...
Remove K Digits - Greedy DSA Problem