3174: Clear-Digits
Easy
table of contents
There have been quite the number of
Since we only delete the first digit and the closest
non-digit character to its left, you could just
iterate through s in reverse and just
not append the letters after a digit character to
ans. (of course, you still have to flip the
string)
code
class Solution {
public:
string clearDigits(string s) {
string ans = "";
int counter = 0;
for (int i = s.size()-1; i >= 0; --i) {
if (s[i]-'0' >= 0 && s[i]-'0' <= 9) {
++counter;
} else if (counter > 0) {
--counter;
} else {
ans += s[i];
}
}
reverse(ans.begin(), ans.end());
return
}
};