Skip to main content

Clear Digits - Solution & Explanation

EasyStringStackSimulation17 min readAsked at: Amazon, Meta, Google +2
Practice this problem

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 <= 100
  • s consists 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.

Code

C

C++

Java

Python

C#

JavaScript

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.

Try this approach in the editor →

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), processing each character a single time.

Space Complexity: O(1), in-place operations over the input string.

Try this approach in the editor →

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

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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

ApproachTimeSpaceWhen to Use
Stack-Based SimulationO(n)O(n)Best general solution. Easy to reason about when removing the most recent character.
Two-Pointer SimulationO(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 is classified as an Easy problem on LeetCode with a high acceptance rate. The challenge mainly tests whether you recognize the stack pattern used to remove the most recent character when processing a string.
Clear Digits Python/Java solution
Python and Java implementations typically use a list or stack-like structure. Iterate through the string, append letters, and pop the last element when a digit appears. This approach keeps the runtime O(n) and the implementation concise.
How to solve Clear Digits in O(n)?
Scan the string from left to right while maintaining a stack or simulated stack. Push letters onto the structure and remove the most recent letter whenever a digit appears. Because every character triggers at most one push or pop, the entire process runs in O(n) time.
What is the best approach for Clear Digits?
The stack-based simulation is the most common solution. Traverse the string, push letters onto a stack, and pop one letter whenever a digit appears. This directly models the rule that a digit removes the closest character to its left. The algorithm runs in O(n) time and O(n) space.
Is Clear Digits asked at Google/Amazon/Meta?
Clear Digits follows a classic stack simulation pattern frequently used in interviews at companies like Amazon, Google, and Meta. While the exact problem may vary, similar questions about removing previous elements or processing strings with stack logic appear often.
What data structure is used in Clear Digits?
A stack is the primary data structure used to solve Clear Digits efficiently. It allows constant-time removal of the most recent character, which matches the rule that each digit deletes the closest letter to its left.
What is the time complexity of Clear Digits?
The optimal time complexity is O(n), where n is the length of the string. Each character is processed exactly once. Stack push and pop operations are constant time, so the overall algorithm remains linear.

Ready to solve this problem?

Practice Clear Digits with our built-in code editor and test cases.

Practice on FleetCode