Clear Digits - Solution & Explanation
Problem Statement
You are given a string s.
Your task is to remove all digits by doing this operation repeatedly:
- Delete the first digit and the closest non-digit character to its left.
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 <= 100sconsists only of lowercase English letters and digits.- The input is generated such that it is possible to delete all digits.
Approach Overview
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.
Approach 1: Stack-Based Approach
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:
- Iterate through each character in the string.
- If the character is a non-digit, push it onto the stack.
- If it's a digit, pop a character from the stack (which would be the closest non-digit to its left).
- Continue this until you have traversed the entire string.
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.
Complexity
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.
Approach 2: Two-Pointer Approach
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:
- Use the first pointer to iterate over each character in the array.
- Utilize a second pointer (to non-digits) to move only for non-digit characters.
- If a digit is found, reduce the non-digit pointer by one to 'remove' the closest non-digit character.
- Finally, reconstruct the string using characters that remain between the two pointers.
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.
Complexity
Time Complexity: O(n), processing each character a single time.
Space Complexity: O(1), in-place operations over the input string.
Approach 3: Stack + Simulation
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.
Code
Python
Java
C++
Go
TypeScript
Complexity Comparison
| 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 | — |
Detailed Complexity Analysis
| 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. |
Video Solution
Clear Digits - Leetcode 3174 - Python • NeetCodeIO • 7,634 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Clear Digits easy or hard?
Clear Digits Python/Java solution
How to solve Clear Digits in O(n)?
What is the best approach for Clear Digits?
Is Clear Digits asked at Google/Amazon/Meta?
What data structure is used in Clear Digits?
What is the time complexity of Clear Digits?
Ready to solve this problem?
Practice Clear Digits with our built-in code editor and test cases.
Practice on FleetCode