// 演算法 / Algorithm:
// 用大小 26 的計數陣列，掃 s 時每個字母 +1，掃 t 時 -1；
// 若兩字串是異位詞，計數最終全部歸零。長度不同則直接 false。
// Use a size-26 count array: +1 for each letter of s, -1 for each of t.
// If they're anagrams every count returns to 0. Different lengths => false.

#include <string.h>   // 為了 strlen / for strlen
#include <stdbool.h>  // 為了 bool / true / false 型別 / for the bool type

bool isAnagram(char* s, char* t) {
    // strlen 回傳字串長度（不含結尾的 '\0'）/ strlen returns string length (excluding the '\0').
    int lenS = strlen(s);
    int lenT = strlen(t);

    // 長度不同必不可能是異位詞，提早結束省時間 / Different lengths can't be anagrams — early exit.
    if (lenS != lenT) return false;

    // 26 格計數陣列，全部初始化為 0；索引 0..25 對應字母 a..z
    // A 26-slot count array initialized to 0; indices 0..25 map to letters a..z.
    int count[26] = {0};

    // 掃過 s 的每個字元，對應計數 +1
    // Walk every char of s, incrementing its count.
    for (int i = 0; i < lenS; i++) {
        // s[i] 是一個字元；s[i] - 'a' 把字母轉成 0..25 的索引
        // s[i] is a char; s[i] - 'a' converts the letter to an index 0..25.
        count[s[i] - 'a']++;
    }

    // 掃過 t 的每個字元，對應計數 -1
    // Walk every char of t, decrementing its count.
    for (int i = 0; i < lenT; i++) {
        count[t[i] - 'a']--;
    }

    // 檢查是否所有計數都歸零；只要有一格非 0 就不是異位詞
    // Check all counts are zero; any non-zero slot means not an anagram.
    for (int i = 0; i < 26; i++) {
        if (count[i] != 0) return false;
    }

    // 全部歸零，s 和 t 字母組成完全相同 / All zero: identical letter composition.
    return true;
}
