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;
    }
};
相关推荐
苏打豆7 分钟前
Pinocchio源码阅读——刚体运动物理量空间变换及求导
c++·线性代数·算法·矩阵·机器人
m0_3807438722 分钟前
Qt中导航栏实现的详细指南
开发语言·c++
FluxArt22 分钟前
GPT Image 2.5 中文提示词教程:Flux Art 可复用模板
前端·gpt·算法
估值探索者33 分钟前
【Python量化策略实战 #02】多因子合成mom和vol两因子打分合成
开发语言·c++·python·数据挖掘·c#
思麟呀34 分钟前
位运算:从一个整数到一个集合
c语言·数据结构·c++·算法
阜阳山君GEO1 小时前
豆包 SEED 新版本 GEO 优化:信源金字塔、打分体系与避坑落地指南
人工智能·算法
晓天衡宇•评测社区1 小时前
晓天衡宇亮相 2026 云栖大会:迈向更可信的高质量评测
人工智能·算法·机器学习
CQU_JIAKE1 小时前
9.12[a]
算法
CQU_JIAKE1 小时前
9.11【A】
算法
All for pursuit.2 小时前
【分治-3】347.前K个高频元素
数据结构·c++·算法·leetcode