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)

结束了

相关推荐
芝士爱知识a3 小时前
AI 模拟面试怎么做:智蛙公考智能体多轮对话 + 实时追问的工程实现
面试·职场和发展
古城小栈3 小时前
为啥说:训练用BF16,推理用FP16
人工智能·算法·机器学习
KaMeidebaby3 小时前
卡梅德生物技术快报|蛋白 N 端测序在重组贻贝融合蛋白表征中的应用,解决原核表达序列偏移工艺难题
前端·人工智能·物联网·算法·百度
Byron__4 小时前
AI学习_06_短期记忆与长期记忆
人工智能·python·学习
帅次4 小时前
Android 高级工程师面试:Java 基础知识 近1年高频追问 22 题
android·java·面试
浆果02074 小时前
NanoTrack C++ — RK3588 实时目标跟踪
c++·目标跟踪·rk3588
Turbo正则4 小时前
群论在AI中的应用概述
人工智能·算法·抽象代数
ysa0510304 小时前
【并查集】判环
c++·笔记·算法
持力行5 小时前
C/C++ 中的 char*:它标识数组吗?为什么能用下标访问?
c语言·c++
Jerry5 小时前
KeetCode 44. 开发商购买土地
算法