|
| 1 | +--- |
| 2 | +title: 2085.统计出现过一次的公共字符串 |
| 3 | +date: 2024-01-12 19:04:52 |
| 4 | +tags: [题解, LeetCode, 简单, 数组, 哈希表, map, 字符串, 计数] |
| 5 | +--- |
| 6 | + |
| 7 | +# 【LetMeFly】2085.统计出现过一次的公共字符串:哈希表 |
| 8 | + |
| 9 | +力扣题目链接:[https://leetcode.cn/problems/count-common-words-with-one-occurrence/](https://leetcode.cn/problems/count-common-words-with-one-occurrence/) |
| 10 | + |
| 11 | +<p>给你两个字符串数组 <code>words1</code> 和 <code>words2</code> ,请你返回在两个字符串数组中 <strong>都恰好出现一次</strong> 的字符串的数目。</p> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | + |
| 15 | +<p><strong>示例 1:</strong></p> |
| 16 | + |
| 17 | +<pre> |
| 18 | +<b>输入:</b>words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"] |
| 19 | +<b>输出:</b>2 |
| 20 | +<strong>解释:</strong> |
| 21 | +- "leetcode" 在两个数组中都恰好出现一次,计入答案。 |
| 22 | +- "amazing" 在两个数组中都恰好出现一次,计入答案。 |
| 23 | +- "is" 在两个数组中都出现过,但在 words1 中出现了 2 次,不计入答案。 |
| 24 | +- "as" 在 words1 中出现了一次,但是在 words2 中没有出现过,不计入答案。 |
| 25 | +所以,有 2 个字符串在两个数组中都恰好出现了一次。 |
| 26 | +</pre> |
| 27 | + |
| 28 | +<p><strong>示例 2:</strong></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<b>输入:</b>words1 = ["b","bb","bbb"], words2 = ["a","aa","aaa"] |
| 32 | +<b>输出:</b>0 |
| 33 | +<b>解释:</b>没有字符串在两个数组中都恰好出现一次。 |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong>示例 3:</strong></p> |
| 37 | + |
| 38 | +<pre> |
| 39 | +<b>输入:</b>words1 = ["a","ab"], words2 = ["a","a","a","ab"] |
| 40 | +<b>输出:</b>1 |
| 41 | +<b>解释:</b>唯一在两个数组中都出现一次的字符串是 "ab" 。 |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | + |
| 46 | +<p><strong>提示:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li><code>1 <= words1.length, words2.length <= 1000</code></li> |
| 50 | + <li><code>1 <= words1[i].length, words2[j].length <= 30</code></li> |
| 51 | + <li><code>words1[i]</code> 和 <code>words2[j]</code> 都只包含小写英文字母。</li> |
| 52 | +</ul> |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +## 方法一:哈希表 |
| 57 | + |
| 58 | +使用两个哈希表,分别统计两个字符串数组中,每个字符串出现的次数。 |
| 59 | + |
| 60 | +(这样,对于一个字符串,我们就能在$O(1)$的时间复杂度内得到这个字符串在两个字符串数组中出现的次数。) |
| 61 | + |
| 62 | +遍历其中一个哈希表,如果这个字符串在两个哈希表中出现的次数都为$1$,则答案个数$+1$。 |
| 63 | + |
| 64 | ++ 时间复杂度$O(size(words1) + size(words2))$,其中$size(words_i)$为字符串数组$words_i$的字符个数。 |
| 65 | ++ 空间复杂度$O(size(words1) + size(words2))$ |
| 66 | + |
| 67 | +### AC代码 |
| 68 | + |
| 69 | +#### C++ |
| 70 | + |
| 71 | +```cpp |
| 72 | +class Solution { |
| 73 | +public: |
| 74 | + int countWords(vector<string>& words1, vector<string>& words2) { |
| 75 | + unordered_map<string, int> m1, m2; |
| 76 | + for (auto& s : words1) { |
| 77 | + m1[s]++; |
| 78 | + } |
| 79 | + for (auto& s : words2) { |
| 80 | + m2[s]++; |
| 81 | + } |
| 82 | + int ans = 0; |
| 83 | + for (auto&& [str, cnt] : m1) { |
| 84 | + if (cnt == 1 && m2[str] == 1) { |
| 85 | + ans++; |
| 86 | + } |
| 87 | + } |
| 88 | + return ans; |
| 89 | + } |
| 90 | +}; |
| 91 | +``` |
| 92 | +
|
| 93 | +#### Python |
| 94 | +
|
| 95 | +```python |
| 96 | +# from typing import List |
| 97 | +# from collections import defaultdict |
| 98 | +
|
| 99 | +class Solution: |
| 100 | + def countWords(self, words1: List[str], words2: List[str]) -> int: |
| 101 | + m1, m2 = defaultdict(int), defaultdict(int) |
| 102 | + for s in words1: |
| 103 | + m1[s] += 1 |
| 104 | + for s in words2: |
| 105 | + m2[s] += 1 |
| 106 | + ans = 0 |
| 107 | + for s, cnt in m1.items(): |
| 108 | + if cnt == 1 and m2[s] == 1: |
| 109 | + ans += 1 |
| 110 | + return ans |
| 111 | +``` |
| 112 | + |
| 113 | +> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2024/01/12/LeetCode%202085.%E7%BB%9F%E8%AE%A1%E5%87%BA%E7%8E%B0%E8%BF%87%E4%B8%80%E6%AC%A1%E7%9A%84%E5%85%AC%E5%85%B1%E5%AD%97%E7%AC%A6%E4%B8%B2/)哦~ |
| 114 | +> Tisfy:[https://letmefly.blog.csdn.net/article/details/135560255](https://letmefly.blog.csdn.net/article/details/135560255) |
0 commit comments