Watch 10 video solutions for Clear Digits, a easy level problem involving String, Stack, Simulation. This walkthrough by NeetCodeIO has 7,535 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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. |