旅游网站开发的背景,网站建设营销怎么做,浙江网站建设推广公司哪家权威,企业网站开发模型图文章目录 题目思路代码 题目
2085. 统计出现过一次的公共字符串
思路
使用两个哈希表 words1Count 和 words2Count 分别统计两个数组中每个单词的出现次数。然后遍历 words1Count 中的每个单词#xff0c;如果该单词在 words1 中出现了一次#xff0c;且在 words2 中也出… 文章目录 题目思路代码 题目
2085. 统计出现过一次的公共字符串
思路
使用两个哈希表 words1Count 和 words2Count 分别统计两个数组中每个单词的出现次数。然后遍历 words1Count 中的每个单词如果该单词在 words1 中出现了一次且在 words2 中也出现了一次那么就将结果 res 自增 1最终res的值就是答案。
代码
class Solution {
public:int countWords(vectorstring words1, vectorstring words2) {unordered_mapstring, int words1Count;unordered_mapstring, 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;}
};