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)

结束了

相关推荐
有意义2 分钟前
栈数据结构全解析:从实现原理到 LeetCode 实战
javascript·算法·编程语言
顾林海3 分钟前
从0到1搭建Android网络框架:别再让你的请求在"路上迷路"了
android·面试·架构
鹿鹿鹿鹿isNotDefined7 分钟前
逐步手写,实现符合 Promise A+ 规范的 Promise
前端·javascript·算法
Mr_WangAndy8 分钟前
现代C++模板与泛型编程_第4章_remove_all_sequence,integer_sequence,is_union
c++·c++40周年·c++标准库用法
拉不动的猪13 分钟前
前端三大权限场景全解析:设计、实现、存储与企业级实践
前端·javascript·面试
封奚泽优24 分钟前
下降算法(Python实现)
开发语言·python·算法
im_AMBER32 分钟前
算法笔记 16 二分搜索算法
c++·笔记·学习·算法
高洁0134 分钟前
【无标具身智能-多任务与元学习】
神经网络·算法·aigc·transformer·知识图谱
赵文宇(温玉)36 分钟前
不翻墙,基于Rancher极速启动Kubernetes,配置SSO登录,在线环境开放学习体验
学习·kubernetes·rancher
leoufung39 分钟前
逆波兰表达式 LeetCode 题解及相关思路笔记
linux·笔记·leetcode