解法一:统计词频
class Solution {
public:
bool isAnagram(string s, string t) {
int word_s[26] = {0};
int word_t[26] = {0};
cout<<word_t[0]<<endl;
for(int i = 0; i < s.size(); i++){
word_s[int(s[i]-'a')]++;
}
for(int i = 0; i < t.size(); i++){
word_t[int(t[i]-'a')]++;
}
cout<<word_t[0]<<endl;
for(int i = 0; i < 26;i++){
if(word_t[i] != word_s[i]){
return false;
}
}
return true;
}
};
解法二:先排序,然后比较
class Solution {
public:
bool isAnagram(string s, string t) {
sort(s.begin(), s.end());
sort(t.begin(), t.end());
if(s == t){
return true;
}
else{
return false;
}
}
};