Remove K Digits - Solution & Explanation
Problem Statement
Given string num representing a non-negative integer num, and an integer k, return the smallest possible integer after removing k digits from num.
Example 1:
Input: num = "1432219", k = 3 Output: "1219" Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1 Output: "200" Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2 Output: "0" Explanation: Remove all the digits from the number and it is left with nothing which is 0.
Constraints:
1 <= k <= num.length <= 105numconsists of only digits.numdoes not have any leading zeros except for the zero itself.
Approach Overview
Problem Overview: You receive a numeric string num and an integer k. Remove exactly k digits so the resulting number is as small as possible while preserving the relative order of the remaining digits. Leading zeros must be handled carefully, and if all digits are removed the result should be "0".
Approach 1: Brute Force Digit Removal (Exponential time, O(2^n) time, O(n) space)
The naive strategy explores all ways to remove k digits and keeps the smallest resulting number. You recursively choose whether to remove or keep each digit until exactly k removals are made. After constructing each candidate string, normalize leading zeros and compare it against the current minimum. This approach demonstrates the core goal of minimizing the number but quickly becomes impractical because the number of combinations grows exponentially. It only works for very small inputs and mainly serves as a conceptual baseline.
Approach 2: Greedy Monotonic Stack (Optimal) (O(n) time, O(n) space)
The optimal strategy uses a greedy rule: remove digits that create a larger prefix. If a digit is bigger than the next one, deleting it makes the number smaller. A monotonic stack captures this idea efficiently. Iterate through the digits of num. While the stack is not empty, the current digit is smaller than the top of the stack, and you still have removals left (k > 0), pop the stack. This removes a larger digit that would otherwise increase the final number.
Push each processed digit onto the stack. After the scan, if k removals remain, remove digits from the end since the suffix is the largest remaining portion. Finally, build the result string from the stack and trim leading zeros. If the string becomes empty, return "0". The stack effectively maintains digits in increasing order, making this a classic combination of greedy, stack, and string processing techniques.
Recommended for interviews: Interviewers expect the monotonic stack solution. It shows you recognize the greedy property: removing a larger digit before a smaller one always improves the number. The brute force idea proves you understand the objective, but the stack-based O(n) solution demonstrates strong algorithmic thinking and familiarity with monotonic data structures.
Approach 1: Monotonic Stack Approach
This approach uses a stack to build the smallest possible number. We iterate over each digit in the string, and for each digit, we compare it with the top of the stack (if the stack is not empty). If the current digit is smaller than the top of the stack and we still have digits to remove, we pop from the stack. Finally, after the loop, if there are remaining digits to remove, we simply remove them from the end of the constructed stack. This ensures the smallest possible arrangement of the remaining digits.
This Python implementation utilizes a stack to keep track of the most desirable sequence of digits. As the function iterates over each digit, it determines whether the stack should pop any elements based on whether doing so would create a smaller number. After the loop, any remaining digits are removed by slicing off the end of the stack. Leading zeros are removed from the final result, and if this leads to an empty string, we return '0'.
Complexity
Time Complexity: O(n), where n is the number of digits in num, because each digit is processed at most twice (once pushed and once popped).
Space Complexity: O(n), because of the space required for the stack to hold the digits.
Approach 2: Greedy Algorithm
Code
Python
Java
C++
Go
TypeScript
Complexity Comparison
| Approach | Complexity |
|---|---|
| Monotonic Stack Approach | Time Complexity: O(n), where n is the number of digits in |
| Greedy Algorithm | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Digit Removal | O(2^n) | O(n) | Conceptual understanding or very small inputs |
| Greedy Monotonic Stack | O(n) | O(n) | Optimal approach for large inputs and interview settings |
Video Solution
L14. Remove K Digits | Stack and Queue Playlist • take U forward • 158,154 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Remove K Digits easy or hard?
Remove K Digits Python/Java solution
How to solve Remove K Digits in O(n)?
What is the best approach for Remove K Digits?
Is Remove K Digits asked at Google/Amazon/Meta?
What data structure is used in Remove K Digits?
What is the time complexity of Remove K Digits?
Ready to solve this problem?
Practice Remove K Digits with our built-in code editor and test cases.
Practice on FleetCode