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

2023每日刷题(八十九)

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

算法思想

我们可以用两个哈希表 cnt1 和 cnt2 分别统计两个字符串数组中每个字符串出现的次数,然后遍历其中一个哈希表,如果某个字符串在另一个哈希表中出现了一次,且在当前哈希表中也出现了一次,则答案加一。

实现代码

cpp 复制代码
class Solution {
public:
    int countWords(vector<string>& words1, vector<string>& words2) {
        unordered_map<string, int> m1;
        unordered_map<string, int> m2;
        for(auto w: words1) {
            m1[w]++;
        }
        for(auto w: words2) {
            m2[w]++;
        }
        int ans = 0;
        for(auto [w, v]: m1) {
            if(v == 1 && m2[w] == 1) {
                ans++;
            }
        }
        return ans;
    }
};

运行结果


之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐
zyl76510 小时前
FlashAttention 详解:从 GPU 硬件、普通 Attention 到分块计算与 Online Softmax
算法
爱读书的小胖14 小时前
LeetCode 122:买卖股票的最佳时机 II(C)解题分析
算法
结局无敌14 小时前
React Native for OpenHarmony 实战:三方库 react-native-modal-dialog 的鸿蒙化适配指南
深度学习·算法·react native·react.js·harmonyos
ychqsq15 小时前
234.无题
经验分享·职场和发展
OPEN-F15 小时前
ROS2系列教程:上位机建图与导航(原理综合)
算法·机器人
weixin_3077791315 小时前
C++代码实现MATLAB中的fitrtree函数功能
开发语言·c++·算法·matlab
AI wins15 小时前
c++新增頭文件
c++
Shadow(⊙o⊙)15 小时前
仿muduo实现高并发服务器——事件就绪管理Channel.hpp
服务器·c++
tomwang032215 小时前
路径优化建模、优化算法分类及工程化小结
算法·数学建模·分类
小羊没烦恼!15 小时前
Ajax与JSON的一些总结
android·java·前端·数据库·mysql·算法·okhttp