程式語言 - LeetCode - C++ - 3120. Count the Number of Special Characters I



題目:


解答:

class Solution {
public:
    int numberOfSpecialChars(string word) {
        unordered_set<char> lower;
        unordered_set<char> upper;

        for (char c : word) {
            if (islower(c)) {
                lower.insert(c);
            }
            else {
                upper.insert(c);
            }
        }

        int ans = 0;

        for (char c : lower) {
            if (upper.count(toupper(c))) {
                ans += 1;
            }
        }

        return ans;
    }
};