You are given a string s.
Your task is to remove all digits by doing this operation repeatedly:
Return the resulting string after removing all digits.
Example 1:
Input: s = "abc"
Output: "abc"
Explanation:
There is no digit in the string.
Example 2:
Input: s = "cb34"
Output: ""
Explanation:
First, we apply the operation on s[2], and s becomes "c4".
Then we apply the operation on s[1], and s becomes "".
Constraints:
1 <= s.length <= 100s consists only of lowercase English letters and digits.Problem Overview: You receive a string containing lowercase letters and digits. Every digit removes itself and the closest non-digit character to its left. After processing the entire string, return the remaining characters.
The key observation: each digit cancels exactly one previous letter. You never need to search far or reorder characters. A structure that supports removing the most recent character works perfectly. This turns the problem into a simple simulation.
Approach 1: Stack-Based Simulation (O(n) time, O(n) space)
Use a stack to keep track of the letters that are still valid. Iterate through the string once. When you encounter a letter, push it onto the stack. When you encounter a digit, pop the top character from the stack because that digit removes the closest letter to its left. This mirrors the rule exactly: the most recent letter is always the one removed.
After the scan completes, the stack contains only the characters that survived all digit removals. Join the stack into a string and return it. The algorithm processes each character once, so the time complexity is O(n). The stack may store up to n characters in the worst case, giving O(n) space complexity.
This approach is intuitive and mirrors the problem statement directly. Many interviewers expect this solution first because the “remove the previous element” pattern naturally maps to a stack.
Approach 2: Two-Pointer In-Place Simulation (O(n) time, O(n) space)
You can simulate the stack using a write pointer over a character array. Convert the string into a mutable buffer and maintain an index that represents the current valid length. Iterate through the characters once. If the character is a letter, write it at the current pointer and move the pointer forward. If the character is a digit, move the pointer one position backward to delete the most recent letter.
This behaves exactly like push and pop operations on a stack, but uses pointer movement instead. The technique is common in two pointer style string processing and avoids explicit stack objects.
Each character is processed once, so the runtime remains O(n). The working buffer uses O(n) space, although the logic conceptually performs the simulation in-place.
Recommended for interviews: The stack-based solution is the most straightforward explanation and usually what interviewers expect first. It clearly shows that you recognized the “remove previous element” pattern common in string and stack problems. The two-pointer simulation demonstrates deeper understanding by replacing the stack with pointer manipulation while keeping the same O(n) performance.
This approach uses a stack data structure to solve the problem. A stack helps us easily keep track of and remove characters because of its LIFO (Last In, First Out) behavior. Here's the detailed plan:
At the end, the stack contains the result after all digits have been removed following the given rule.
This C solution uses an array to simulate a stack. The code iterates over the input string, and for each digit found, it pops the top character from the stack if available. For each non-digit, it pushes the character onto the stack. At the end, the stack contents are the result string.
Time Complexity: O(n), where n is the length of the string, because each character is processed once.
Space Complexity: O(n), in the worst case where all characters are non-digits, and they are stored in the stack.
This approach utilizes two pointers to effectively manage the character positions during the removal process without using a stack. It provides an intuitive way to keep track of characters to be removed as we traverse the string:
This C solution uses two index variables—one iterates over each character while the other builds the result string in place by moving non-digit characters and 'removing' digits by decrementing the non-digit index.
Time Complexity: O(n), processing each character a single time.
Space Complexity: O(1), in-place operations over the input string.
We use a stack stk to simulate this process. We traverse the string s. If the current character is a digit, we pop the top element from the stack. Otherwise, we push the current character into the stack.
Finally, we concatenate the elements in the stack into a string and return it.
The time complexity is O(n), and the space complexity is O(n). Where n is the length of the string s.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Stack-Based Approach | Time Complexity: O(n), where n is the length of the string, because each character is processed once. Space Complexity: O(n), in the worst case where all characters are non-digits, and they are stored in the stack. |
| Two-Pointer Approach | Time Complexity: O(n), processing each character a single time. Space Complexity: O(1), in-place operations over the input string. |
| Stack + Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Stack-Based Simulation | O(n) | O(n) | Best general solution. Easy to reason about when removing the most recent character. |
| Two-Pointer Simulation | O(n) | O(n) | When you want stack behavior using pointer manipulation or in-place style string processing. |
Clear Digits - Leetcode 3174 - Python • NeetCodeIO • 7,535 views views
Watch 9 more video solutions →Practice Clear Digits with our built-in code editor and test cases.
Practice on FleetCode