Remove K Digits


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.
โ 1. Problem Explanation (In Simple Terms)
You're given:
A non-negative number in string format (e.g.,
"1432219"
)An integer
k
= number of digits to remove
Your task:
Remove exactly k
digits to make the smallest possible number.
๐ง Return the result as a string with no leading zeros.
If the result is empty โ return "0"
.
๐ก 2. Key Insights
We want to remove digits to get the smallest number, which means:
We should remove bigger digits when a smaller digit follows them
This is a greedy problem โ remove left-side peaks
Use a monotonic stack:
Push digits into the stack
Before pushing, pop from the stack while:
Top is greater than current digit
And we still have
k
digits to remove
After the loop:
If
k > 0
โ remove extra digits from end of stackJoin stack to form result, remove leading zeros
๐ง 3. Steps to Solve
Initialize an empty
stack = []
Iterate over each digit in the input number:
While
stack
is non-empty and top > current digit andk > 0
โ popPush current digit to the stack
After iteration, if
k > 0
โ pop from end of stackk
timesBuild the result from the stack:
Join characters
Remove leading zeros
If empty โ return
"0"
โ 4. JavaScript Code (Clean & Optimized)
/**
* @param {string} num
* @param {number} k
* @return {string}
*/
function removeKdigits(num, k) {
const stack = [];
for (const digit of num) {
while (stack.length > 0 && k > 0 && stack[stack.length - 1] > digit) {
stack.pop();
k--;
}
stack.push(digit);
}
// If k still > 0, remove from end
while (k > 0) {
stack.pop();
k--;
}
// Convert to string and remove leading zeros
const result = stack.join('').replace(/^0+/, '');
return result === '' ? '0' : result;
}
๐ 5. Dry Run Example
Input:
num = "1432219", k = 3
Step-by-step:
โ Remove '4' โ smaller '3' coming
โ Remove '3' โ smaller '2' coming
โ Remove '2' โ final result: [1, 2, 1, 9]
Stack after cleanup โ "1219"
โ
Output: "1219"
Another example:
num = "10200", k = 1
โ Remove '1'? No (it's smallest)
โ Remove '2'? Yes โ stack: [0, 0]
โ
Output: "200" โ remove leading zeros โ "200"
โฑ๏ธ 6. Time & Space Complexity
Time Complexity: O(n)
Each digit is pushed and popped at most onceSpace Complexity: O(n)
For the stack
โ Final Verdict
Classic greedy + monotonic stack problem
Great for showcasing digit manipulation and stack thinking
Clean, linear time solution with minimal edge case handling
Subscribe to my newsletter
Read articles from Abhi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
