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)

结束了

相关推荐
hanlin0341 分钟前
刷题笔记:力扣第84题-柱状图中最大的矩形
笔记·算法·leetcode
青少儿编程课堂1 小时前
威佐夫博弈(双堆取子游戏)解析
c++·python·算法·bfs·信息学竞赛
再吃一根胡萝卜6 小时前
08 · 前端:Vue3 + SSE 流式与执行链路可视化
面试
辰烨chenye6 小时前
LeetCode Hot 100 题解 · 普通数组篇
算法·leetcode·职场和发展
再吃一根胡萝卜6 小时前
03 · 后端:FastAPI 与分层架构
面试
hqyjzsb9 小时前
零 AI 项目经验,学 Python 转型 AI 的正确顺序是什么?
开发语言·人工智能·python·算法·职场和发展·数据挖掘·数据分析
辰烨chenye9 小时前
LeetCode Hot 100 题解 · 二分篇
java·算法·leetcode
微功夫信息技术9 小时前
分层多智能体强化学习驱动的非急救转运公平 - 效率统一调度系统研究与实践
人工智能·学习·算法·动态规划
sanjiaomao33310 小时前
从论文公式到Python实现:用单元测试校验滑动平均算法
python·算法·单元测试
Keven_1110 小时前
算法札记:ACM适用的很骚的C++语法(持续更新)
开发语言·c++