Clear Digits (LeetCode 3174): Solution and Explanation
Each digit in s deletes itself and the closest letter to its left that is still present. Return what is left once every digit is gone. For cb34 the answer is the empty string, and abc stays abc.
Intuition
The rule says to always handle the first digit. That means digits are processed left to right, exactly as if you were typing s one key at a time.
Read that way, a digit is a backspace key: it removes the last letter currently on screen.
The screen behaves like a stack. Letters are pushed, a digit pops the top, and the final stack is the answer.
Approach: Stack as a typing buffer
- Create an empty stack (a list, StringBuilder, string or byte slice).
- For each character
chins: if it is a digit, pop the top of the stack. - Otherwise push
ch. - Join the stack from bottom to top and return it.
Step-by-step example
s = "cb34"
cis a letter: push it. Screenc.bis a letter: push it. Screencb.3is a digit: it backspaces overb. Screenc.4is a digit: it backspaces overc. Screen empty.- No characters remain, so the answer is the empty string.
Code: Python, Java, C++
1class Solution:
2 def clearDigits(self, s: str) -> str:
3 stack = []
4 for ch in s:
5 if ch.isdigit():
6 stack.pop()
7 else:
8 stack.append(ch)
9 return "".join(stack)1class Solution {
2 public String clearDigits(String s) {
3 StringBuilder stack = new StringBuilder();
4 for (char ch : s.toCharArray()) {
5 if (Character.isDigit(ch)) {
6 stack.deleteCharAt(stack.length() - 1);
7 } else {
8 stack.append(ch);
9 }
10 }
11 return stack.toString();
12 }
13}1class Solution {
2public:
3 string clearDigits(string s) {
4 string stack;
5 for (char ch : s) {
6 if (isdigit(ch)) {
7 stack.pop_back();
8 } else {
9 stack.push_back(ch);
10 }
11 }
12 return stack;
13 }
14};Time and space complexity
O(n)
Each character causes one push or one pop, and the final join is linear.
O(n)
A string with no digits keeps every letter on the stack.
Other approaches
In-place write pointer (time O(n), space O(1) extra)
Use a character array and a write index as the stack top. A letter is written at the index and the index moves right; a digit moves it one step left. Return the prefix before the index.
Literal simulation (time O(n^2), space O(n))
Find the first digit, delete it and the letter before it, and repeat. With n at most 100 this passes, but each deletion shifts the string, so it is quadratic.
Common mistakes
- Deleting the character just before the digit in the original string rather than the closest letter that is still present. In
ab12the2must removea, not the already deletedb. - Building the answer by repeated string slicing in a language with immutable strings, which hides a quadratic cost.
- Returning
nullinstead of an empty string when every letter gets erased.
Frequently asked questions
Why can the digits be processed left to right in one pass?
The rule always takes the first remaining digit, so digits are handled in left-to-right order. Each one only looks at letters to its left, which are exactly the letters already read.
How is this different from Removing Stars From a String?
It is the same mechanism with digits in place of stars. Removing Stars allows inputs up to 100,000 characters, so the linear stack matters more there.
Can a digit ever find the stack empty?
No. The problem guarantees that every digit has a letter to delete, so a pop is always safe.