Bit solution takes only 12 ms in c++


  • -1
    H
    bool isAnagram(string s, string t) {
        if (s.length() != t.length()) return false;
        
        // For string s to add each bit.
        // For string t to minus each bit.
        int items[26];
        memset(items, 0, sizeof(items));
        for (int i=0; i < s.length(); ++i) {
            int index1 = s[i] - 'a';
            int index2 = t[i] - 'a';
            ++ items[index1];
            -- items[index2];
        }
        
        // If all zero, then it is an anagram
        bool isA = true;
        for (int i=0; i<26; ++i) {
            if (items[i] != 0) {
                isA = false; break;
            }
        }
        
        return isA;
    }

  • 0
    B

    Excuse me, is that "bit" solution?


Log in to reply
 

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