Skip to content

Commit 62574e0

Browse files
authored
Merge pull request #184 from LetMeFly666/2085
添加了问题“2085.统计出现过一次的公共字符串”的代码和题解
2 parents 366de7b + f09df1e commit 62574e0

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-01-12 19:04:59
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-01-12 19:06:15
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int countWords(vector<string>& words1, vector<string>& words2) {
14+
unordered_map<string, int> m1, m2;
15+
for (auto& s : words1) {
16+
m1[s]++;
17+
}
18+
for (auto& s : words2) {
19+
m2[s]++;
20+
}
21+
int ans = 0;
22+
for (auto&& [str, cnt] : m1) {
23+
if (cnt == 1 && m2[str] == 1) {
24+
ans++;
25+
}
26+
}
27+
return ans;
28+
}
29+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-01-12 19:07:38
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-01-12 19:08:56
6+
'''
7+
from typing import List
8+
from collections import defaultdict
9+
10+
class Solution:
11+
def countWords(self, words1: List[str], words2: List[str]) -> int:
12+
m1, m2 = defaultdict(int), defaultdict(int)
13+
for s in words1:
14+
m1[s] += 1
15+
for s in words2:
16+
m2[s] += 1
17+
ans = 0
18+
for s, cnt in m1.items():
19+
if cnt == 1 and m2[s] == 1:
20+
ans += 1
21+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@
421421
|2042.检查句子中的数字是否递增|简单|<a href="https://leetcode.cn/problems/check-if-numbers-are-ascending-in-a-sentence/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/01/03/LeetCode%202042.%E6%A3%80%E6%9F%A5%E5%8F%A5%E5%AD%90%E4%B8%AD%E7%9A%84%E6%95%B0%E5%AD%97%E6%98%AF%E5%90%A6%E9%80%92%E5%A2%9E/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128538008" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-numbers-are-ascending-in-a-sentence/solutions/2043292/letmefly-2042jian-cha-ju-zi-zhong-de-shu-5leg/" target="_blank">LeetCode题解</a>|
422422
|2048.下一个更大的数值平衡数|中等|<a href="https://leetcode.cn/problems/next-greater-numerically-balanced-number/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/12/09/LeetCode%202048.%E4%B8%8B%E4%B8%80%E4%B8%AA%E6%9B%B4%E5%A4%A7%E7%9A%84%E6%95%B0%E5%80%BC%E5%B9%B3%E8%A1%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134900679" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/next-greater-numerically-balanced-number/solutions/2560353/letmefly-2048xia-yi-ge-geng-da-de-shu-zh-7p4t/" target="_blank">LeetCode题解</a>|
423423
|2050.并行课程III|困难|<a href="https://leetcode.cn/problems/parallel-courses-iii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/07/28/LeetCode%202050.%E5%B9%B6%E8%A1%8C%E8%AF%BE%E7%A8%8BIII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131973511" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/parallel-courses-iii/solutions/2362142/letmefly-2050bing-xing-ke-cheng-iiidfs-b-9tuc/" target="_blank">LeetCode题解</a>|
424+
|2085.统计出现过一次的公共字符串|简单|<a href="https://leetcode.cn/problems/count-common-words-with-one-occurrence/solutions/" target="_blank">题目地址</a>|<a href="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/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135560255" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-common-words-with-one-occurrence/solutions/2601942/letmefly-2085tong-ji-chu-xian-guo-yi-ci-cdg6x/" target="_blank">LeetCode题解</a>|
424425
|2100.适合打劫银行的日子|中等|<a href="https://leetcode.cn/problems/find-good-days-to-rob-the-bank/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/09/26/LeetCode%202100.%E9%80%82%E5%90%88%E6%89%93%E5%8A%AB%E9%93%B6%E8%A1%8C%E7%9A%84%E6%97%A5%E5%AD%90/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133324938" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-good-days-to-rob-the-bank/solutions/2460364/letmefly-2100gua-he-da-jie-yin-xing-de-r-aqwp/" target="_blank">LeetCode题解</a>|
425426
|2105.给植物浇水II|中等|<a href="https://leetcode.cn/problems/watering-plants-ii/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2022/09/05/LeetCode%202105.%E7%BB%99%E6%A4%8D%E7%89%A9%E6%B5%87%E6%B0%B4II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126709258" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/watering-plants-ii/solution/by-tisfy-0erl/" target="_blank">LeetCode题解</a>|
426427
|2106.摘水果|困难|<a href="https://leetcode.cn/problems/maximum-fruits-harvested-after-at-most-k-steps/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/05/04/LeetCode%202106.%E6%91%98%E6%B0%B4%E6%9E%9C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130482457" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-fruits-harvested-after-at-most-k-steps/solutions/2255124/letmefly-2106zhai-shui-guo-by-tisfy-tb8v/" target="_blank">LeetCode题解</a>|
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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>给你两个字符串数组&nbsp;<code>words1</code>&nbsp;&nbsp;<code>words2</code>&nbsp;,请你返回在两个字符串数组中 <strong>都恰好出现一次</strong>&nbsp;的字符串的数目。</p>
12+
13+
<p>&nbsp;</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>&nbsp;</p>
45+
46+
<p><strong>提示:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= words1.length, words2.length &lt;= 1000</code></li>
50+
<li><code>1 &lt;= words1[i].length, words2[j].length &lt;= 30</code></li>
51+
<li><code>words1[i]</code> 和&nbsp;<code>words2[j]</code>&nbsp;都只包含小写英文字母。</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

Comments
 (0)