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)

结束了

相关推荐
掘金安东尼4 小时前
让 JavaScript 更容易「善后」的新能力
前端·javascript·面试
掘金安东尼4 小时前
用 HTMX 为 React Data Grid 加速实时更新
前端·javascript·面试
灵感__idea6 小时前
Hello 算法:众里寻她千“百度”
前端·javascript·算法
UrbanJazzerati7 小时前
非常友好的Vue 3 生命周期详解
前端·面试
想用offer打牌14 小时前
高并发下如何保证接口的幂等性
后端·面试·状态机
Wect16 小时前
LeetCode 130. 被围绕的区域:两种解法详解(BFS/DFS)
前端·算法·typescript
牛奶1 天前
Vue 基础理论 & API 使用
前端·vue.js·面试
牛奶1 天前
Vue 底层原理 & 新特性
前端·vue.js·面试
NAGNIP1 天前
一文搞懂深度学习中的通用逼近定理!
人工智能·算法·面试
青青家的小灰灰1 天前
深入理解事件循环:异步编程的基石
前端·javascript·面试