[leetcode_242]Valid Anagram

判断两个字符串是否含有相同的字母且字母个数相同。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    bool isAnagram(string s, string t) {
        vector<int> sCount(26, 0);
        for (int i = 0;i < s.length();i++)
        {
            sCount[s[i] - 'a']++;
        }
        for (int i = 0;i < t.length();i++)
        {
            sCount[t[i] - 'a']--;
        }
        for (int i = 0;i < sCount.size(); i++)
        {
            if (0 != sCount[i])
            {
                return false;
            }
        }
        return true;
    }
};
Licensed under CC BY-NC-SA 4.0