2025-02-10 Daily Challenge
Today I have done leetcode's February LeetCoding Challenge with cpp
.
February LeetCoding Challenge 10
Description
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.
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.
Solution
class Solution {
public:
string clearDigits(string s) {
string answer;
for(auto c : s) {
if(isalpha(c)) {
answer.push_back(c);
} else {
if(answer.size()) answer.pop_back();
}
}
return answer;
}
};
// Accepted
// 688/688 cases passed (0 ms)
// Your runtime beats 100 % of cpp submissions
// Your memory usage beats 87.14 % of cpp submissions (8.4 MB)