网站从哪些方面来做,定制制作网站哪家好,vps centos wordpress,wordpress禁止保存图片LeetCode
2085.统计出现过一次的公共字符串
2085. 统计出现过一次的公共字符串 - 力扣#xff08;LeetCode#xff09;
题目描述
给你两个字符串数组 words1 和 words2 #xff0c;请你返回在两个字符串数组中 都恰好出现一次 的字符串的数目。
示例 1#xff1a;
输…LeetCode
2085.统计出现过一次的公共字符串
2085. 统计出现过一次的公共字符串 - 力扣LeetCode
题目描述
给你两个字符串数组 words1 和 words2 请你返回在两个字符串数组中 都恰好出现一次 的字符串的数目。
示例 1
输入words1 [leetcode,is,amazing,as,is], words2 [amazing,leetcode,is]
输出2
解释
- leetcode 在两个数组中都恰好出现一次计入答案。
- amazing 在两个数组中都恰好出现一次计入答案。
- is 在两个数组中都出现过但在 words1 中出现了 2 次不计入答案。
- as 在 words1 中出现了一次但是在 words2 中没有出现过不计入答案。
所以有 2 个字符串在两个数组中都恰好出现了一次。示例 2
输入words1 [b,bb,bbb], words2 [a,aa,aaa]
输出0
解释没有字符串在两个数组中都恰好出现一次。示例 3
输入words1 [a,ab], words2 [a,a,a,ab]
输出1
解释唯一在两个数组中都出现一次的字符串是 ab 。提示
1 words1.length, words2.length 10001 words1[i].length, words2[j].length 30words1[i] 和 words2[j] 都只包含小写英文字母。
思路
哈希表
代码
C
class Solution {
public:int countWords(vectorstring words1, vectorstring words2) {unordered_mapstring, int w1;unordered_mapstring, int w2;for(string word : words1){w1[word];}for(string word : words2){w2[word];}int ans 0;for(string word : words1){if(w1[word] 1 w2[word] 1 ){ans;}}return ans;}
};Java
class Solution {public int countWords(String[] words1, String[] words2) {// 统计字符串出现频率MapString, Integer w1 new HashMap();MapString, Integer w2 new HashMap();for(String w : words1){w1.put(w,w1.getOrDefault(w,0) 1);}for(String w : words2){w2.put(w,w2.getOrDefault(w,0) 1);}// 遍历 words1 出现的字符串并判断是否满足要求int ans 0;for(String w : w1.keySet()){if(w1.getOrDefault(w,0) 1 w2.getOrDefault(w,0) 1){ans;}}return ans;}
}