[LeetCode] Clear Digits

3174. Clear Digits

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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
string clearDigits(string s) {
string res = "";
for(auto& ch : s) {
if(isdigit(ch)) {
if(res.size()) res.pop_back();
} else res.push_back(ch);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/06/08/PS/LeetCode/clear-digits/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.