LC 2085. 统计出现过一次的公共字符串

2085. 统计出现过一次的公共字符串

难度 : 简单

题目大意:

给你两个字符串数组 words1words2 ,请你返回在两个字符串数组中 都恰好出现一次 的字符串的数目。

提示:

  • 1 <= words1.length, words2.length <= 1000
  • 1 <= words1[i].length, words2[j].length <= 30
  • words1[i]words2[j] 都只包含小写英文字母。

哈希表记数

用哈希表记录下word1word2中的字符串的数量,最后判断一下是不是都出现一次即可

cpp 复制代码
class Solution {
public:
    int countWords(vector<string>& words1, vector<string>& words2) {
        unordered_map<string, int> cnt1, cnt2;
        for (const string str : words1) {
            ++ cnt1[str];
        }
        for (const string str : words2) {
            ++ cnt2[str];
        }
        int res = 0;
        for (const string str : words1) {
            if (cnt1[str] == 1 and cnt2[str] == 1)
                res ++;
        }
        return res;
    }
};

时间复杂度; O ( 2 ∗ n + m ) O(2 * n + m) O(2∗n+m)

结束了

相关推荐
vortex57 分钟前
几种 dump hash 方式对比分析
算法·哈希算法
2501_9418649619 分钟前
科学方法论破解学习时间堆砌误区
学习
初願致夕霞38 分钟前
Linux_进程
linux·c++
Thera7771 小时前
【Linux C++】彻底解决僵尸进程:waitpid(WNOHANG) 与 SA_NOCLDWAIT
linux·服务器·c++
Wei&Yan1 小时前
数据结构——顺序表(静/动态代码实现)
数据结构·c++·算法·visual studio code
1024小神1 小时前
SVG标签中path路径参数学习
学习
wregjru1 小时前
【QT】4.QWidget控件(2)
c++
浅念-2 小时前
C++入门(2)
开发语言·c++·经验分享·笔记·学习
ZH15455891312 小时前
Flutter for OpenHarmony Python学习助手实战:面向对象编程实战的实现
python·学习·flutter
小羊不会打字2 小时前
CANN 生态中的跨框架兼容桥梁:`onnx-adapter` 项目实现无缝模型迁移
c++·深度学习