Post

3447 Clear Digits

3447 Clear Digits

Clear Digits image

You are given a string s.

Your task is to remove all digits by doing this operation repeatedly:

1
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
2
3
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

func clearDigits(s string) string {
    st := make([]rune,0)
    for _ , k := range s {
        _, err := strconv.Atoi(string(k))
        if err == nil {
            if len(st) > 0 {
                st = st[:len(st)-1]
            }
        } else {
            st = append(st , k)
        }
    }

    m := ""
    for _, k := range st {
        m += string(k)
    }
    return m
}



This post is licensed under CC BY 4.0 by the author.