O
问题
解决方案
代码
/*
思路: 字符串匹配问题, 利用哈希表和遍历解决。
- define numsone numstwo maps to save both data
- for iterator i in nums:
- - if numsone[i] = numstwo[i] contine
- - else break;
- return false;
*/
class Solution {
public:
bool isAnagram(string s, string t) {
unordered_map<char,int> numone, numtwo;
for(char a: s){
numone[a]++;
}
for(char a: t){
numtwo[a]++;
}
if(numone.size()<numtwo.size()){
unordered_map<char,int> temp = numone;
numone = numtwo;
numtwo = temp;
}
for(unordered_map<char,int>::iterator i = numone.begin();i!=numtwo.end();i++){
if(numone[(*i).first] == numtwo[(*i).first]){
continue;
}
else return false;
}
return true;
}
};
总结与反思
- 注意交换问题。