// 演算法 / Algorithm:
// 同樣用大小 26 的計數陣列，s 的字母 +1、t 的字母 -1，
// 最後若全為 0 即為異位詞。長度不同直接回傳 false。
// Same size-26 count array: +1 for s's letters, -1 for t's letters.
// All zeros at the end means anagram; different lengths => false.

#include <string>
#include <array>   // 為了 std::array 固定大小陣列 / for std::array, a fixed-size array
using namespace std;

class Solution {
public:
    bool isAnagram(string s, string t) {
        // string 的 .size() 回傳字元數 / .size() returns the number of characters.
        // 長度不同必不可能是異位詞 / Different lengths can't be anagrams.
        if (s.size() != t.size()) return false;

        // std::array<int,26> 是固定 26 格的陣列，{} 讓所有元素初始化為 0
        // std::array<int,26> is a fixed 26-element array; {} zero-initializes all of them.
        array<int, 26> count{};

        // range-for：依序取出 s 的每個字元 c（無需手動管理索引）
        // Range-for: iterate each character c of s without manual indexing.
        for (char c : s) {
            // c - 'a' 把字母轉成 0..25 的索引，對應計數 +1
            // c - 'a' maps the letter to index 0..25; increment that count.
            count[c - 'a']++;
        }

        // 對 t 的每個字元，對應計數 -1
        // For each character of t, decrement the matching count.
        for (char c : t) {
            count[c - 'a']--;
        }

        // 檢查是否所有計數都為 0；任一非 0 表示字母數量不一致
        // Verify every count is 0; any non-zero means letter counts differ.
        for (int v : count) {
            if (v != 0) return false;
        }

        // 全部歸零 → 是異位詞 / All zero -> it's an anagram.
        return true;
    }
};
