C++ 5 lines O(n) solution


  • 0
    bool isAnagram(string s, string t) {
            if (s.length() != t.length()) { return false; }
            int freqs[26] = {}; 
            for (char c : s) { freqs[c - 'a']++; }
            for (char c : t) { if (--freqs[c - 'a'] < 0) { return false; }}
            return true;
    }
    

Log in to reply
 

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