c++ using unordered_map (O(n))


  • 0
    bool isAnagram(string s, string t) {
        unordered_map<char, int> m;
        for (int i = 0; i < s.size(); ++i) m[s[i]]++;
        for (int i = 0; i < t.size(); ++i) {
            if (m.find(t[i]) == m.end()) return false;
            if (--m[t[i]] == 0) m.erase(t[i]);
        }
        return m.empty();
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.