C++ 4 and 5line, 89ms and 9ms


  • -1
    Y
    class Solution {
    public:
        bool isAnagram(string s, string t) {
            sort(s.begin(),s.end());
            sort(t.begin(),t.end());
            if (!s.compare(t))  return true;
            return false;
        }
    };
    
    class Solution {
    public:
        bool isAnagram(string s, string t) {
            int i,a[26]={0};
            for (i=0;i<s.size();++i)    ++a[s[i]-'a'];
            for (i=0;i<t.size();++i)    --a[t[i]-'a'];
            for (i=0;i<26;++i)  if (a[i]) return false;
            return true;
        }
    };
    

  • -1
    X

    int a[26]={0};
    You should not initialize a int array in this way. You are right here because the the default value is zero; when you try this:
    int a[26] = {1}, only a[0] is initialized as 1, the others are still zeros.


Log in to reply
 

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