> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Clear Digits Python Solution with Tests

> Tested Python solution for LeetCode 3174 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 3174, [Easy](/catalog/easy). Topics: [String](/catalog/topics/string), [Stack](/catalog/topics/stack), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/clear-digits/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 3174   # by problem number
lcpy gen -s clear_digits   # by problem name
```

## Problem

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.

**Note** that the operation *cannot* be performed on a digit that does not have any non-digit character to its left.

### Examples

```
Input: s = "abc"
Output: "abc"
Explanation: There is no digit in the string.
```

```
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.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/clear_digits/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/clear_digits/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def clear_digits(self, s: str) -> str:
        stack: list[str] = []
        for char in s:
            if char.isdigit():
                stack.pop()
            else:
                stack.append(char)
        return "".join(stack)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.