Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C++ - 3121. Count the Number of Special Characters II
題目:

解答:
class Solution {
public:
int numberOfSpecialChars(string word) {
unordered_map<char, int> lower;
unordered_map<char, int> upper;
for (int i = 0; i < word.size(); ++i) {
char c = word[i];
if (islower(c)) {
lower[c] = i;
}
else if (upper.count(c) == 0) {
upper[c] = i;
}
}
int ans = 0;
for (auto [k, v] : lower) {
char c = toupper(k);
if (upper.count(c) && upper[c] > v) {
ans += 1;
}
}
return ans;
}
};