LC 2085. 统计出现过一次的公共字符串

2085. 统计出现过一次的公共字符串

难度 : 简单

题目大意:

给你两个字符串数组 words1 和 words2 ,请你返回在两个字符串数组中 都恰好出现一次 的字符串的数目。

提示:

  • 1 <= words1.length, words2.length <= 1000
  • 1 <= words1[i].length, words2[j].length <= 30
  • words1[i] 和 words2[j] 都只包含小写英文字母。

哈希表记数

用哈希表记录下word1和word2中的字符串的数量,最后判断一下是不是都出现一次即可

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 小时前
这个十几个agent 协作项目值得关注
人工智能·面试·github
码客-ygluu3 小时前
X3 LUA 异步分布式微服务集群框架
c++·架构·lua
~kiss~4 小时前
A Survey of On-Policy Distillation for LLMs 学习(一)
学习
forest_095 小时前
多 Agent 协作第二课:交叉验证≠独立验证
人工智能·python·算法·机器学习·prompt·agi
用户47949283569155 小时前
从一句话或代码仓到可交互 HTML:开源 Agent 画图技能 Archify v3.2 实测与选型边界
前端·后端·面试
小杨小杨、努力变强!5 小时前
python学习笔记
笔记·学习
九英里路6 小时前
protobuf快速上手
网络·c++·中间件·json·谷歌·序列化·protobuf
纪念 2296 小时前
C++ 算法基础 (差分)
开发语言·c++·算法
文丑颜不良啊9 小时前
LeetCode2761~2780题目解析
java·leetcode
jyOverQ9 小时前
从零学 JVM(六):垃圾回收算法详解——标记清除、标记复制与标记整理
jvm·算法