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)

结束了

相关推荐
然我5 分钟前
路由还能这么玩?从懒加载到路由守卫,手把手带你解锁 React Router 进阶技巧
前端·react.js·面试
ysa0510306 分钟前
竞赛常用加速技巧#模板
c++·笔记·算法
7 979 分钟前
C语言基础知识--文件的顺序读写与随机读写
java·数据结构·算法
2401_841003989 分钟前
Kubernetes 资源管理全解析
算法·贪心算法
exm-zem1 小时前
多用户图书管理系统
c++
☆璇1 小时前
【数据结构】排序
c语言·开发语言·数据结构·算法·排序算法
我要成为c嘎嘎大王1 小时前
【C++】初识C++(1)
开发语言·c++
云和数据.ChenGuang2 小时前
gitlab-ci.yml
面试·职场和发展·gitee·运维面试题·运维技术总结
LGGGGGQ3 小时前
嵌入式学习-PyTorch(4)-day21
学习
艾莉丝努力练剑4 小时前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表