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)

结束了

相关推荐
OOJO4 小时前
c++---list介绍
c语言·开发语言·数据结构·c++·算法·list
别或许6 小时前
1、高数----函数极限与连续(知识总结)
算法
派大星~课堂6 小时前
【力扣-142. 环形链表2 ✨】Python笔记
python·leetcode·链表
田梓燊6 小时前
code 560
数据结构·算法·哈希算法
笨笨饿6 小时前
29_Z变换在工程中的实际意义
c语言·开发语言·人工智能·单片机·mcu·算法·机器人
kobesdu6 小时前
综合强度信息的激光雷达去拖尾算法解析和源码实现
算法·机器人·ros·slam·激光雷达
Amazing_Cacao6 小时前
深度观察 | 从“产区玄学”到“液态战场”:精品巧克力的终极试金石
学习
weixin_413063216 小时前
记录 MeshFlow-Online-Video-Stabilization 在线稳像
算法·meshflow·实时防抖
会编程的土豆7 小时前
【数据结构与算法】动态规划
数据结构·c++·算法·leetcode·代理模式
炘爚7 小时前
深入解析printf缓冲区与fork进程复制机制
linux·运维·算法