统计出现过一次的公共字符串
解题思路:
显然我们需要统计每个字符串数组中每个字符串出现的字数
使用哈希表key表示字符产,val用来记录该字符串出现的次数
最后遍历map1,要找到每个字符串只出现一次,并且在两个字符串数组中都出现
i.second == 1 && map2[i.first] == 1
即可进行判断
class Solution {
public:
int countWords(vector<string>& words1, vector<string>& words2) {
int res = 0;
unordered_map<string,int> map1;
unordered_map<string,int> map2;
for(auto &word : words1){
map1[word]++;
}
for(auto &word : words2){
map2[word]++;
}
for(auto &i : map1){
if(i.second == 1 && map2[i.first] == 1) res++;
}
return res;
}
};