cpp solution with hash


  • 0
    bool isAnagram(string s, string t) 
    {
        int hs[26] = {0};
        
        for (auto elem : s)
            hs[elem - 'a'] ++;
        
        for (auto elem : t)
            if (hs[elem - 'a'] > 0)
                hs[elem - 'a'] --;                
            else
                return false;
        
        for (int i = 0; i < 26; i++)
            if (hs[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.