文章目录
- 题目
 - 思路
 - 代码
 
题目
2085. 统计出现过一次的公共字符串
 
思路
使用两个哈希表 words1Count 和 words2Count 分别统计两个数组中每个单词的出现次数。然后遍历 words1Count 中的每个单词,如果该单词在 words1 中出现了一次,且在 words2 中也出现了一次,那么就将结果 res 自增 1,最终res的值就是答案。
代码
class Solution {
public:
    int countWords(vector<string>& words1, vector<string>& words2) {
        unordered_map<string, int> words1Count;
        unordered_map<string, int> words2Count;
        int res = 0;
        for (auto& word : words1) {
            words1Count[word]++;
        }
        for (auto& word : words2) {
            words2Count[word]++;
        }
        for (auto& x : words1Count) {
            if (x.second == 1 && words2Count[x.first] == 1) {
                res++;
            }
        }
        return res;
    }
};
                
















