【Leetcode】2085. 统计出现过一次的公共字符串

文章目录

题目

2085. 统计出现过一次的公共字符串
在这里插入图片描述

思路

使用两个哈希表 words1Count 和 words2Count 分别统计两个数组中每个单词的出现次数。然后遍历 words1Count 中的每个单词,如果该单词在 words1 中出现了一次,且在 words2 中也出现了一次,那么就将结果 res 自增 1,最终res的值就是答案。

代码

class Solution {
public:
    int countWords(vector<string>& words1, vector<string>& words2) {
        unordered_map<string, int> words1Count;
        unordered_map<string, int> words2Count;
        int res = 0;
        for (auto& word : words1) {
            words1Count[word]++;
        }
        for (auto& word : words2) {
            words2Count[word]++;
        }
        for (auto& x : words1Count) {
            if (x.second == 1 && words2Count[x.first] == 1) {
                res++;
            }
        }
        return res;
    }
};

相关推荐

  1. 2085. 统计出现公共字符串

    2024-01-13 01:56:01       67 阅读
  2. LC 2085. 统计出现公共字符串

    2024-01-13 01:56:01       50 阅读
  3. 2085. 统计出现公共字符串

    2024-01-13 01:56:01       37 阅读
  4. 力扣2085.统计出现公共字符串

    2024-01-13 01:56:01       57 阅读
  5. 统计出现公共字符串

    2024-01-13 01:56:01       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-13 01:56:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-13 01:56:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-13 01:56:01       87 阅读
  4. Python语言-面向对象

    2024-01-13 01:56:01       96 阅读

热门阅读

  1. LeetCode878. Nth Magical Number

    2024-01-13 01:56:01       53 阅读
  2. vue3中el-table实现表格合计行

    2024-01-13 01:56:01       66 阅读
  3. [ECE]1.3 Basic logic operations

    2024-01-13 01:56:01       48 阅读
  4. 3 微信小程序

    2024-01-13 01:56:01       51 阅读
  5. 面试题-回溯算法解法模板

    2024-01-13 01:56:01       55 阅读
  6. 数据库面经---10则

    2024-01-13 01:56:01       61 阅读
  7. Android实现通过字符串找到图片、Class

    2024-01-13 01:56:01       49 阅读
  8. [ECE] Introduction to Digital Logic and Systems

    2024-01-13 01:56:01       52 阅读
  9. 【PHP】判断字符串是否是有效的base64编码

    2024-01-13 01:56:01       56 阅读
  10. golang中context详解

    2024-01-13 01:56:01       57 阅读