242. Valid Anagram

2018-04-16  本文已影响15人  安东可

242. Valid Anagram
【思路】

    bool isAnagram(string s, string t) {
        sort(s.begin(),s.end());
        sort(t.begin(),t.end());
        if(s == t)
            return true;
        return false;
                                        
        
    }

或者:

    bool isAnagram(string s, string t) {
        vector<int> count(26, 0);
        for(int i = 0; i < s.size(); i ++)
            count[s[i]-'a'] ++;
        for(int i = 0; i < t.size(); i ++)
            count[t[i]-'a'] --;
        for(int i = 0; i < 26; i ++)
            if(count[i] != 0)
                return false;
        return true;
    }
上一篇 下一篇

猜你喜欢

热点阅读