程式語言 - LeetCode - CPP - 242. Valid Anagram



題目:


解答:

class Solution {
public:
    bool isAnagram(string s, string t) {
        vector<int> cnt(26, 0);

        for (char ch : s) {
            cnt[ch - 'a'] += 1;
        }

        for (char ch : t) {
            cnt[ch - 'a'] -= 1;
        }

        for (int i = 0; i <26; ++i) {
            if (cnt[i]) {
                return false;
            }
        }

        return true;
    }
};