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)

结束了

相关推荐
sunny_6 分钟前
📖 2026年 大厂前端面试手写题库已开源(2.3k star)
前端·面试·github
小璐资源网15 分钟前
C++中如何正确区分`=`和`==`的使用场景?
java·c++·算法
N1_WEB22 分钟前
HDU:杭电 2018 复试真题汇总
算法
AMoon丶30 分钟前
C++模版-函数模版,类模版基础
java·linux·c语言·开发语言·jvm·c++·算法
王知无(import_bigdata)32 分钟前
一个极简的AI Agentic Engineering技术栈学习路线
人工智能·学习
We་ct37 分钟前
LeetCode 79. 单词搜索:DFS回溯解法详解
前端·算法·leetcode·typescript·深度优先·个人开发·回溯
眼眸流转1 小时前
LeetCode热题100(四)
算法·leetcode·职场和发展
y = xⁿ1 小时前
【从零开始学习Redis|第七篇】Redis 进阶原理篇:消息队列、分布式锁、缓存击穿与事务实现
java·redis·学习·缓存
相信神话20211 小时前
第零章:新手的第一课:正确认知游戏开发
大数据·数据库·算法·2d游戏编程·godot4·2d游戏开发
式5161 小时前
VLLM架构学习(一)VLLM是什么、VLLM的原理
学习·vllm