Simple C++ solution with O(n) time complexity


  • 1
    L
    bool isAnagram(string s, string t) {
            int m=s.size(), n=t.size();
            if (m != n) return false;
            
            int freq[26] = {0};
            for(int i=0; i<m; i++){
                freq[s[i]-'a']++;
                freq[t[i]-'a']--;
            }
            
            for(int i=0; i<26; i++)
                if (freq[i])    return false;
            return true;
        }

Log in to reply
 

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