leetcode: 242. Valid Anagram
class Solution {
public:
bool isAnagram(string s, string t) {
if (s.size() != t.size()) return false;
int mp[26] = {0};
for (char x : s) {
mp[x - 'a']++;
}
for (char x : t) {
mp[x - 'a']--;
if (mp[x - 'a'] < 0) return false;
}
return true;
}
};
还有种写法是:
class Solution {
public:
bool isAnagram(string s, string t) {
int mp[26] = {0};
for (char x : s) mp[x - 'a']++;
for (char x : t) mp[x - 'a']--;
for (int i = 0; i < 26; i++)
if (mp[i] != 0)
return false;
return true;
}
};