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)

结束了

相关推荐
fie888926 分钟前
NSCT(非下采样轮廓波变换)的分解和重建程序
算法
玄斎35 分钟前
MySQL 单表操作通关指南:建库 / 建表 / 插入 / 增删改查
运维·服务器·数据库·学习·程序人生·mysql·oracle
用户47949283569151 小时前
面试官问"try-catch影响性能吗",我用数据打脸
前端·javascript·面试
晨晖21 小时前
单链表逆转,c语言
c语言·数据结构·算法
沐雪架构师1 小时前
大模型Agent面试精选15题(第四辑)-Agent与RAG(检索增强生成)结合的高频面试题
面试·职场和发展
未若君雅裁1 小时前
JVM面试篇总结
java·jvm·面试
kk哥88992 小时前
C++ 对象 核心介绍
java·jvm·c++
helloworddm2 小时前
WinUI3 主线程不要执行耗时操作的原因
c++
YoungHong19922 小时前
面试经典150题[072]:从前序与中序遍历序列构造二叉树(LeetCode 105)
leetcode·面试·职场和发展
无能者狂怒2 小时前
YOLO C++ Onnx Opencv项目配置指南
c++·opencv·yolo