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 小时前
【MySQL 零基础入门】事务精讲(二):ACID 特性与并发问题
数据库·笔记·学习·mysql
Hard but lovely2 小时前
C++11: 自定义异常&&标准异常体系&&回顾c异常处理方式
开发语言·c++
happyhappy没有句号3 小时前
嵌入式单片机一套通关学习笔记
笔记·单片机·嵌入式硬件·学习
悠哉悠哉愿意3 小时前
【嵌入式学习笔记】工程模板建立
笔记·嵌入式硬件·学习
jianfeng_zhu3 小时前
整数数组匹配
数据结构·c++·算法
Chrikk3 小时前
现代化 C++ 工程构建:CMake 与包管理器的依赖治理
开发语言·c++
ozyzo3 小时前
例题
c++
smj2302_796826523 小时前
解决leetcode第3782题交替删除操作后最后剩下的整数
python·算法·leetcode
d111111111d3 小时前
STM32外设基地址与寄存器偏移地址的深度解析
笔记·stm32·单片机·嵌入式硬件·学习
踏浪无痕3 小时前
从 Guava ListenableFuture 学习生产级并发调用实践
后端·面试·架构