Skip to main content
Back to problems
Leetcode
Medium
Strings
Stacks
Clear Digits

Remove every digit and the nearest non-digit character to its left, then return the remaining string.

Acceptance 0%
Problem Statement

You are given a string containing lowercase letters and digits. Process the string from left to right, and whenever you encounter a digit, remove that digit and also remove the closest character to its left that has not already been removed.

After all digits have been processed, return the remaining string.

The relative order of the characters that remain should stay the same as in the original string.

Input Format

  • A single string s consisting of lowercase English letters and digits.
  • You should process the entire string according to the removal rule described above.

Output Format

  • Return the final string after removing all digits and the matched characters to their left.

Constraints

  • 1 <= s.length <= $10^{5}$
  • s contains only lowercase English letters and digits.
  • There is always a valid character to remove when a digit is encountered in the intended problem setting.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "cb34"

Output

""

Explanation

3 removes b, 4 removes c, leaving nothing.

Example 2

Input

s = "abc"

Output

"abc"

Explanation

There are no digits, so nothing is removed.

Show 1 more example

Example 3

Input

s = "a1b2c3"

Output

""

Explanation

Each digit removes the closest remaining character to its left: 1 removes a, 2 removes b, and 3 removes c.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.