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)

结束了

相关推荐
lisw056 小时前
稀疏算法概述!
算法·机器人·无人机
思想觉悟7 小时前
Aosp实战之自定义开机启动进程
c++·面试
YLCHUP7 小时前
【括号匹配+dp优化】题解:双端括号(brackets)_动态规划_状态优化_数学建模_贪心_性质挖掘_STLmap_C++算法竞赛
数据结构·c++·笔记·算法·数学建模·贪心算法·动态规划
郝学胜-神的一滴8 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
console.log('npc')11 小时前
Python第9课 类学习笔记,前端js语法对照
前端·python·学习
geovindu12 小时前
python:Logistic Regression
开发语言·后端·python·算法
geovindu13 小时前
python: Random Forest
开发语言·后端·python·算法·随机森林
derek199714 小时前
软考软件设计师:主键、传递依赖和冗余,三问其实是一件事
c语言·c++·计算机网络·软件工程·学习方法
凌冰_14 小时前
数据结构 - 线性结构(三)
java·数据结构·算法·软件工程
拂拉氏15 小时前
【知识讲解】 数据结构与算法-树状数组的认识
数据结构·算法·树状数组