给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
本题的第一思路就是采用字典,分别统计两个字符串每个字符出现的次数,如果是异构的则最后字典的所有个数为0
class Solution {
public:
bool isAnagram(string s, string t) {
// 本题的思路采用map
unordered_map<char, int> m;
for (auto c: s) {
++m[c];
}
for (auto c: t) {
--m[c];
}
// unordered_map<char, int> iterator it;
for (auto it = m.begin(); it != m.end(); it++ ) {
if(it->second != 0) {
return false;
}
}
return true;
}
};