LeetCode2085. Count Common Words With One Occurrence

文章目录

一、题目

Given two string arrays words1 and words2, return the number of strings that appear exactly once in each of the two arrays.

Example 1:

Input: words1 = "leetcode","is","amazing","as","is", words2 = "amazing","leetcode","is"

Output: 2

Explanation:

  • "leetcode" appears exactly once in each of the two arrays. We count this string.
  • "amazing" appears exactly once in each of the two arrays. We count this string.
  • "is" appears in each of the two arrays, but there are 2 occurrences of it in words1. We do not count this string.
  • "as" appears once in words1, but does not appear in words2. We do not count this string.
    Thus, there are 2 strings that appear exactly once in each of the two arrays.
    Example 2:

Input: words1 = "b","bb","bbb", words2 = "a","aa","aaa"

Output: 0

Explanation: There are no strings that appear in each of the two arrays.

Example 3:

Input: words1 = "a","ab", words2 = "a","a","a","ab"

Output: 1

Explanation: The only string that appears exactly once in each of the two arrays is "ab".

Constraints:

1 <= words1.length, words2.length <= 1000

1 <= words1i.length, words2j.length <= 30

words1i and words2j consists only of lowercase English letters.

二、题解

cpp 复制代码
class Solution {
public:
    int countWords(vector<string>& words1, vector<string>& words2) {
        unordered_map<string,int> map1;
        unordered_map<string,int> map2;
        for(auto w1:words1){
            map1[w1]++;
        }
        for(auto w2:words2){
            map2[w2]++;
        }
        int res = 0;
        for(auto w1:words1){
            if(map1[w1] == 1 && map2[w1] == 1) res++;
        }
        return res;
    }
};
相关推荐
8Qi83 小时前
LeetCode 75:颜色分类(荷兰国旗问题)—— Java 题解 ✅
java·算法·leetcode·指针·排序
888CC++5 小时前
如何在 C 语言中进行程序调试?
前端·javascript·算法
pluviophile_s6 小时前
数据结构:第2讲:线性表
数据结构·笔记
(●—●)橘子……6 小时前
力扣第503场周赛练习理解
python·学习·算法·leetcode·职场和发展·周赛
明志数科8 小时前
4D时序标注技术详解:让机器人理解连续动作的数据基础
java·算法·机器人
feng_you_ying_li8 小时前
C++复习二,继承与多态
c++
小小de风呀8 小时前
de风——【从零开始学C++】(十一):list的基本使用和模拟实现
开发语言·c++·list
KaMeidebaby8 小时前
卡梅德生物技术快报|原核表达系统工艺优化:包涵体重折叠 + 分子筛纯化实现功能 RBD 高效制备,附全参数配置
前端·人工智能·算法·数据挖掘·数据分析
陌路208 小时前
C++高级进阶--夯实进阶基础(1)
开发语言·c++