leetcode-ブラシ問題-242(C++)

622 ワード

2つの文字列sとtを与え、tがsのアルファベット異位語であるか否かを判断する関数を記述する.
本題の第1の構想は辞書を採用して、それぞれ2つの文字列の各文字の出現回数を統計して、もし異性ならば最後の辞書のすべての数は0です
class Solution {
public:
    bool isAnagram(string s, string t) {
        //        map
        unordered_map m;
        for (auto c: s) {
            ++m[c];
        }
        for (auto c: t) {
            --m[c];
        }
        // unordered_map iterator it;
        for (auto it = m.begin(); it != m.end(); it++ ) {
            if(it->second != 0) {
                return false;
            }
        }
        return true;

    }
};