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)

结束了

相关推荐
A_humble_scholar3 分钟前
Linux(十七)深入多线程编程:同步原语与实战指南
linux·运维·c++
一楼的猫8 分钟前
网文AI辅助怎么过审?番茄200+维度检测与去AI味实战
人工智能·学习·机器学习·自然语言处理·ai写作
小李飞刀李寻欢17 分钟前
DeepSeek V3 版本模型结构分析
算法·大模型·deepseek
晨米酱24 分钟前
Matt Pocock Skills:整套设计只为一个目标
面试·架构·agent
黄敬峰27 分钟前
从零用 Vue 3 + DeepSeek 搭建一个 AI 流式聊天机器人
面试
某不知名網友30 分钟前
C++ 七大排序算法完整讲解
java·算法·排序算法
得物技术1 小时前
得物推荐系统诊断 Agent:从 “调接口” 到 “会思考”|AICon 演讲整理
人工智能·算法·架构
渣渣灰飞1 小时前
MySQL 系统学习 第五阶段:企业级 MySQL 实战开发 第一章:企业数据库设计规范
数据库·学习·mysql
Lugas1 小时前
为啥说男生找对象尽量在25岁前找到?
算法
MrZhao4001 小时前
从能跑到可用:一个 Agent Harness 还差哪些工程闭环?
算法