// 演算法 / Algorithm:
// 與 C 版相同：掃一次字串記錄出現過的大小寫字母，
// 最後統計同時擁有大小寫兩種形式的字母個數。
// Same as the C version: one pass to record seen letters, then count
// letters that appear in both lowercase and uppercase.
class Solution {
public:
    int numberOfSpecialChars(string word) {
        // vector<bool> 是會自動管理記憶體的布林陣列；初值全部設為 false
        // vector<bool> is a self-managing boolean array; all elements start as false
        vector<bool> seenLower(26, false);
        vector<bool> seenUpper(26, false);

        // range-for：依序取出 word 裡的每個字元，比手動索引更簡潔
        // range-for: iterate each character of word in order, cleaner than manual indexing
        for (char c : word) {
            if (islower(c))                  // <cctype> 的函式，判斷是否為小寫 / from <cctype>, true if lowercase
                seenLower[c - 'a'] = true;   // 標記這個小寫字母 / mark this lowercase letter
            else                             // 其餘為大寫 / the rest are uppercase
                seenUpper[c - 'A'] = true;   // 標記這個大寫字母 / mark this uppercase letter
        }

        int count = 0;                       // 特殊字母計數 / count of special letters
        for (int i = 0; i < 26; i++)         // 逐一檢查 26 個字母 / check each of the 26 letters
            if (seenLower[i] && seenUpper[i])// 大小寫都出現 → 特殊 / both cases present → special
                count++;

        return count;                        // 回傳特殊字母數量 / return how many are special
    }
};
