Remove K Digits - Problem
Given a string num representing a non-negative integer and an integer k, your task is to remove exactly k digits from the number to create the smallest possible integer.

The goal is to strategically remove digits to minimize the resulting number. For example, from "1432219" removing 3 digits, we want to get "1219" (not "4329" or "1432").

Key Points:
  • You must remove exactly k digits
  • The result should be the smallest possible number
  • Leading zeros should be removed from the final result
  • If all digits are removed, return "0"

Input & Output

example_1.py โ€” Basic Case
$ Input: num = "1432219", k = 3
โ€บ Output: "1219"
๐Ÿ’ก Note: Remove digits '4', '3', and '2' (the first '2') to get the smallest result. The optimal strategy is to remove larger digits that appear before smaller ones.
example_2.py โ€” All Same Digits
$ Input: num = "10200", k = 1
โ€บ Output: "200"
๐Ÿ’ก Note: Remove the leading '1' to get the smallest possible number. Since we want the smallest result, we remove the leftmost non-zero digit.
example_3.py โ€” Remove All Digits
$ Input: num = "9", k = 1
โ€บ Output: "0"
๐Ÿ’ก Note: When all digits are removed, return "0" as specified in the problem statement.

Visualization

Tap to expand
Remove K Digits - Visual StrategyOriginal: 1 4 3 2 2 1 9Remove 3 digits to get smallest numberGreedy Process (Left to Right):Step 1: Keep '1' (stack: [1])Step 2: Keep '4' since 1 < 4 (stack: [1,4])Step 3: Remove '4', keep '3' since 4 > 3 (stack: [1,3]) โœ‚๏ธStep 4: Remove '3', keep '2' since 3 > 2 (stack: [1,2]) โœ‚๏ธStep 5: Keep '2' since 2 = 2 (stack: [1,2,2])Step 6: Keep '1' since 2 > 1 (stack: [1,2,2,1])Step 7: Remove last digit '2' to satisfy k=3 (stack: [1,2,1,9]) โœ‚๏ธFinal Result"1219"๐ŸŽฏ Key Insight: Always remove larger digits that appear before smaller ones to minimize the result
Understanding the Visualization
1
Scan Left to Right
Process each digit from left to right, as position matters for final value
2
Greedy Removal
When you see a smaller digit, remove any larger digits before it (while you have removals left)
3
Maintain Stack
Keep a stack of digits in non-decreasing order to build the optimal result
4
Handle Edge Cases
Remove from end if needed, clean leading zeros, return '0' if empty
Key Takeaway
๐ŸŽฏ Key Insight: The monotonic stack approach greedily removes larger digits when smaller ones appear later, ensuring we get the lexicographically smallest result in optimal O(n) time.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string, each element pushed and popped at most once

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Stack space for storing the result digits

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค num.length โ‰ค 104
  • 0 โ‰ค k โ‰ค num.length
  • num consists of only digits
  • num does not have any leading zeros except for the zero itself
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
32.5K Views
Medium Frequency
~25 min Avg. Time
987 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen