// 演算法 / Algorithm:
// 掃字串一次，用兩個布林陣列分別記下出現過的小寫與大寫字母；
// 最後數出有幾個字母編號同時在兩個陣列中為真。
// Scan once, mark seen lowercase/uppercase letters in two boolean arrays,
// then count letter indices that are true in BOTH arrays.

int numberOfSpecialChars(char* word) {
    bool seenLower[26] = {false};   // 26 個小寫字母看過沒 / has each lowercase letter been seen
    bool seenUpper[26] = {false};   // 26 個大寫字母看過沒 / has each uppercase letter been seen

    // 從頭掃到字串結尾的 '\0'（C 字串以 '\0' 結束）
    // Walk until the terminating '\0' (C strings end with '\0')
    for (int i = 0; word[i] != '\0'; i++) {
        char c = word[i];                  // 取出目前字元 / current character
        if (c >= 'a' && c <= 'z')          // 是小寫字母嗎 / is it lowercase?
            seenLower[c - 'a'] = true;     // c-'a' 把 a..z 變成 0..25 並標記 / map a..z to 0..25 and mark
        else                               // 否則一定是大寫（題目保證只有字母）/ otherwise uppercase (input is all letters)
            seenUpper[c - 'A'] = true;     // c-'A' 把 A..Z 變成 0..25 並標記 / map A..Z to 0..25 and mark
    }

    int count = 0;                         // 特殊字母計數 / number of special letters
    for (int i = 0; i < 26; i++)           // 檢查 26 個字母編號 / check all 26 letter indices
        if (seenLower[i] && seenUpper[i])  // 同一字母大小寫都出現過 / both cases of this letter appeared
            count++;                       // 是特殊字母，加一 / it is special, count it

    return count;                          // 回傳結果 / return the answer
}
