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)

结束了

相关推荐
库森学长4 分钟前
Kafka为什么这么快?
后端·面试·kafka
CodeWithMe12 分钟前
【C/C++】EBO空基类优化介绍
开发语言·c++
凌辰揽月25 分钟前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
Dignity_呱26 分钟前
vue3对组件通信做了哪些升级?
前端·vue.js·面试
就是我29 分钟前
开发“业务组件库”,该从哪里入手?
前端·javascript·面试
lifallen31 分钟前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
jingfeng51432 分钟前
数据结构排序
数据结构·算法·排序算法
k要开心32 分钟前
从C到C++语法过度1
开发语言·c++
whoarethenext43 分钟前
使用 C/C++的OpenCV 实时播放火柴人爱心舞蹈动画
c语言·c++·opencv
能工智人小辰1 小时前
Codeforces Round 509 (Div. 2) C. Coffee Break
c语言·c++·算法