← All problems

3174. Clear Digits

EasyOpen on LeetCodeProblem statement

Problem Statement

3174. Clear Digits

Easy


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:

Java

Source file
class Solution {
    public String clearDigits(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++)
            if (Character.isDigit(s.charAt(i)))
                sb.setLength(sb.length() - 1);
            else sb.append(s.charAt(i));
        return sb.toString();
    }
}