7 lines C, 0 ms


  • 5

    Just the normal counting method. I chose to support full ASCII instead of just lower case letters because it's less code (I don't want to subtract 'a' all the time, might also be faster without doing that).

    bool isAnagram(char* s, char* t) {
        int ctr[128] = {};
        while (*s) ++ctr[*s++];
        while (*t) --ctr[*t++];
        for (int i=0; i<128; i++)
            if (ctr[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.