Do not sort input strings


  • 0
    W
    bool isAnagram(string s, string t) {
        if (s.size() != t.size())
            return false;
            
        int stat[26] = {0};
        for (int i = 0; i < s.size(); i++) {
            ++stat[s[i]-'a'];
            --stat[t[i]-'a'];
        }
        for (int i = 0; i < 26; i++) {
            if (stat[i] != 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.