|
| 1 | +--- |
| 2 | +id: remove-k-digits |
| 3 | +title: Remove K Digits (LeetCode) |
| 4 | +sidebar_label: 0402-RemoveKDigits |
| 5 | +description: Find the smallest possible number by removing K digits from the given number. |
| 6 | +--- |
| 7 | + |
| 8 | +## Problem Description |
| 9 | + |
| 10 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 11 | +| :---------------- | :------------ | :--------------- | |
| 12 | +| [Remove K Digits](https://leetcode.com/problems/remove-k-digits/) | [Remove K Digits Solution on LeetCode](https://leetcode.com/problems/remove-k-digits/solutions/) | [vaishu_1904](https://leetcode.com/u/vaishu_1904/) | |
| 13 | + |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +Given a string `num` representing a non-negative integer and an integer `k`, return the smallest possible number you can get by removing `k` digits from `num`. |
| 17 | + |
| 18 | +**Example:** |
| 19 | + |
| 20 | +#### Example 1 |
| 21 | + |
| 22 | +- **Input:** `num = "1432219"`, `k = 3` |
| 23 | +- **Output:** `"1219"` |
| 24 | +- **Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest. |
| 25 | + |
| 26 | +#### Example 2 |
| 27 | + |
| 28 | +- **Input:** `num = "10200"`, `k = 1` |
| 29 | +- **Output:** `"200"` |
| 30 | +- **Explanation:** Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes. |
| 31 | + |
| 32 | +#### Example 3 |
| 33 | + |
| 34 | +- **Input:** `num = "10"`, `k = 2` |
| 35 | +- **Output:** `"0"` |
| 36 | +- **Explanation:** Remove all the digits from the number and it is left with nothing which is 0. |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- `1 <= num.length <= 10^5` |
| 41 | +- `num` consists of only digits. |
| 42 | +- `num` does not have any leading zeros except for the zero itself. |
| 43 | +- `1 <= k <= num.length` |
| 44 | + |
| 45 | +### Approach |
| 46 | + |
| 47 | +To solve this problem, we need to systematically remove digits to achieve the smallest possible number. Here's a step-by-step approach: |
| 48 | + |
| 49 | +1. **Initialization**: |
| 50 | + - Use a stack to build the smallest possible number. |
| 51 | + - Iterate through each digit in `num`. |
| 52 | + |
| 53 | +2. **Stack Operations**: |
| 54 | + - For each digit, while the stack is not empty, and the top of the stack is greater than the current digit, and `k` is greater than 0, pop the stack (remove the top element). |
| 55 | + - Push the current digit to the stack. |
| 56 | + - Decrement `k` each time a digit is removed. |
| 57 | + |
| 58 | +3. **Remove Remaining Digits**: |
| 59 | + - If there are still digits to remove (`k > 0`), remove them from the end of the stack. |
| 60 | + |
| 61 | +4. **Build the Result**: |
| 62 | + - Convert the stack to a string. |
| 63 | + - Remove leading zeros. |
| 64 | + - If the result is empty, return "0". |
| 65 | + |
| 66 | +### Solution Code |
| 67 | + |
| 68 | +#### Python |
| 69 | + |
| 70 | +```python |
| 71 | +class Solution: |
| 72 | + def removeKdigits(self, num: str, k: int) -> str: |
| 73 | + stack = [] |
| 74 | + for digit in num: |
| 75 | + while k > 0 and stack and stack[-1] > digit: |
| 76 | + stack.pop() |
| 77 | + k -= 1 |
| 78 | + stack.append(digit) |
| 79 | + |
| 80 | + # Remove the remaining digits from the end if k > 0 |
| 81 | + stack = stack[:-k] if k else stack |
| 82 | + |
| 83 | + # Build the result and remove leading zeros |
| 84 | + result = ''.join(stack).lstrip('0') |
| 85 | + |
| 86 | + return result if result else "0" |
| 87 | +``` |
| 88 | + |
| 89 | +#### Java |
| 90 | +```java |
| 91 | +import java.util.*; |
| 92 | + |
| 93 | +class Solution { |
| 94 | + public String removeKdigits(String num, int k) { |
| 95 | + Deque<Character> stack = new ArrayDeque<>(); |
| 96 | + for (char digit : num.toCharArray()) { |
| 97 | + while (k > 0 && !stack.isEmpty() && stack.peekLast() > digit) { |
| 98 | + stack.removeLast(); |
| 99 | + k--; |
| 100 | + } |
| 101 | + stack.addLast(digit); |
| 102 | + } |
| 103 | + |
| 104 | + // Remove the remaining digits from the end if k > 0 |
| 105 | + for (int i = 0; i < k; ++i) { |
| 106 | + stack.removeLast(); |
| 107 | + } |
| 108 | + |
| 109 | + // Build the result and remove leading zeros |
| 110 | + StringBuilder result = new StringBuilder(); |
| 111 | + boolean leadingZero = true; |
| 112 | + for (char digit : stack) { |
| 113 | + if (leadingZero && digit == '0') continue; |
| 114 | + leadingZero = false; |
| 115 | + result.append(digit); |
| 116 | + } |
| 117 | + |
| 118 | + return result.length() == 0 ? "0" : result.toString(); |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +#### C++ |
| 124 | +```c++ |
| 125 | +#include <string> |
| 126 | +#include <deque> |
| 127 | +using namespace std; |
| 128 | + |
| 129 | +class Solution { |
| 130 | +public: |
| 131 | + string removeKdigits(string num, int k) { |
| 132 | + deque<char> stack; |
| 133 | + for (char digit : num) { |
| 134 | + while (k > 0 && !stack.empty() && stack.back() > digit) { |
| 135 | + stack.pop_back(); |
| 136 | + k--; |
| 137 | + } |
| 138 | + stack.push_back(digit); |
| 139 | + } |
| 140 | + |
| 141 | + // Remove the remaining digits from the end if k > 0 |
| 142 | + for (int i = 0; i < k; ++i) { |
| 143 | + stack.pop_back(); |
| 144 | + } |
| 145 | + |
| 146 | + // Build the result and remove leading zeros |
| 147 | + string result; |
| 148 | + bool leadingZero = true; |
| 149 | + for (char digit : stack) { |
| 150 | + if (leadingZero && digit == '0') continue; |
| 151 | + leadingZero = false; |
| 152 | + result += digit; |
| 153 | + } |
| 154 | + |
| 155 | + return result.empty() ? "0" : result; |
| 156 | + } |
| 157 | +}; |
| 158 | +``` |
| 159 | +
|
| 160 | +### Conclusion |
| 161 | +The solution efficiently finds the smallest possible number by removing k digits using a stack-based approach. This approach ensures that we maintain the smallest possible number at each step, and handles edge cases like leading zeros and empty results effectively. |
0 commit comments