Leetcode—2085. 统计出现过一次的公共字符串【简单】
2023每日刷题(八十九)
Leetcode—2085. 统计出现过一次的公共字符串

算法思想
我们可以用两个哈希表 cnt1 和 cnt2 分别统计两个字符串数组中每个字符串出现的次数,然后遍历其中一个哈希表,如果某个字符串在另一个哈希表中出现了一次,且在当前哈希表中也出现了一次,则答案加一。
实现代码
class Solution {
public:int countWords(vector<string>& words1, vector<string>& words2) {unordered_map<string, int> m1;unordered_map<string, int> m2;for(auto w: words1) {m1[w]++;}for(auto w: words2) {m2[w]++;}int ans = 0;for(auto [w, v]: m1) {if(v == 1 && m2[w] == 1) {ans++;}}return ans;}
};
运行结果

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!