Talentd
FleetCode

3174. Clear Digits

Easy82.7% acceptance5 companies

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.

Hints open one at a time

Hint 1
Process string s from left to right, if s[i] is a digit, mark the nearest unmarked non-digit index to its left.
Hint 2
Delete all digits and all marked characters.
7:42
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def clearDigits(self, s: str) -> str:
3
Starter codePython 3Spaces: 4
Examples
Input
"abc"
Input
"cb34"
Your code is saved on this device.Solution