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)

结束了

相关推荐
ShineWinsu33 分钟前
对于C++:IO流状态的详细解析
c++·面试·io·cin·io流状态·goodbit·failbit
「PlanA」1 小时前
测试开发面经001
面试
数模竞赛Paid answer1 小时前
2026年中青杯数学建模A题数学建模论文智能评估系统与多智能体优化方法求解全过程论文及程序
算法·数学建模·中青杯
银-豆豆2 小时前
数据结构与算法-动态规划、回溯与贪心
算法·动态规划
JL152 小时前
面试项目被问到自闭-如何把课设包装成亮眼项目
面试·职场和发展
Lzg_na2 小时前
constexpr的优势
c++
想吃火锅10053 小时前
【leetcode】200. 岛屿数量
算法·leetcode·职场和发展
王维同学3 小时前
进程模块枚举、映像身份与线程启动地址关联
c++·windows·安全
hold?fish:palm3 小时前
24 回文链表
数据结构·c++·链表
举手4 小时前
Dispatcher模块剖析
linux·c++