Skip to content

Commit b5b461b

Browse files
authored
feat: add solutions to lc problem: No.2085 (doocs#2209)
No.2085.Count Common Words With One Occurrence
1 parent ea5d866 commit b5b461b

File tree

7 files changed

+141
-65
lines changed

7 files changed

+141
-65
lines changed

solution/2000-2099/2085.Count Common Words With One Occurrence/README.md

+48-23
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56-
**方法一:哈希表**
56+
**方法一:哈希表计数**
5757

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

6060
时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是两个字符串数组的长度。
6161

@@ -70,7 +70,7 @@ class Solution:
7070
def countWords(self, words1: List[str], words2: List[str]) -> int:
7171
cnt1 = Counter(words1)
7272
cnt2 = Counter(words2)
73-
return sum(cnt2[k] == 1 for k, v in cnt1.items() if v == 1)
73+
return sum(v == 1 and cnt2[w] == 1 for w, v in cnt1.items())
7474
```
7575

7676
### **Java**
@@ -80,24 +80,22 @@ class Solution:
8080
```java
8181
class Solution {
8282
public int countWords(String[] words1, String[] words2) {
83-
Map<String, Integer> cnt1 = count(words1);
84-
Map<String, Integer> cnt2 = count(words2);
83+
Map<String, Integer> cnt1 = new HashMap<>();
84+
Map<String, Integer> cnt2 = new HashMap<>();
85+
for (var w : words1) {
86+
cnt1.merge(w, 1, Integer::sum);
87+
}
88+
for (var w : words2) {
89+
cnt2.merge(w, 1, Integer::sum);
90+
}
8591
int ans = 0;
86-
for (String w : words1) {
87-
if (cnt1.getOrDefault(w, 0) == 1 && cnt2.getOrDefault(w, 0) == 1) {
92+
for (var e : cnt1.entrySet()) {
93+
if (e.getValue() == 1 && cnt2.getOrDefault(e.getKey(), 0) == 1) {
8894
++ans;
8995
}
9096
}
9197
return ans;
9298
}
93-
94-
private Map<String, Integer> count(String[] words) {
95-
Map<String, Integer> cnt = new HashMap<>();
96-
for (String w : words) {
97-
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
98-
}
99-
return cnt;
100-
}
10199
}
102100
```
103101

@@ -109,10 +107,16 @@ public:
109107
int countWords(vector<string>& words1, vector<string>& words2) {
110108
unordered_map<string, int> cnt1;
111109
unordered_map<string, int> cnt2;
112-
for (auto& w : words1) cnt1[w]++;
113-
for (auto& w : words2) cnt2[w]++;
110+
for (auto& w : words1) {
111+
++cnt1[w];
112+
}
113+
for (auto& w : words2) {
114+
++cnt2[w];
115+
}
114116
int ans = 0;
115-
for (auto& w : words1) ans += (cnt1[w] == 1 && cnt2[w] == 1);
117+
for (auto& [w, v] : cnt1) {
118+
ans += v == 1 && cnt2[w] == 1;
119+
}
116120
return ans;
117121
}
118122
};
@@ -121,7 +125,7 @@ public:
121125
### **Go**
122126
123127
```go
124-
func countWords(words1 []string, words2 []string) int {
128+
func countWords(words1 []string, words2 []string) (ans int) {
125129
cnt1 := map[string]int{}
126130
cnt2 := map[string]int{}
127131
for _, w := range words1 {
@@ -130,13 +134,34 @@ func countWords(words1 []string, words2 []string) int {
130134
for _, w := range words2 {
131135
cnt2[w]++
132136
}
133-
ans := 0
134-
for _, w := range words1 {
135-
if cnt1[w] == 1 && cnt2[w] == 1 {
137+
for w, v := range cnt1 {
138+
if v == 1 && cnt2[w] == 1 {
136139
ans++
137140
}
138141
}
139-
return ans
142+
return
143+
}
144+
```
145+
146+
### **TypeScript**
147+
148+
```ts
149+
function countWords(words1: string[], words2: string[]): number {
150+
const cnt1 = new Map<string, number>();
151+
const cnt2 = new Map<string, number>();
152+
for (const w of words1) {
153+
cnt1.set(w, (cnt1.get(w) ?? 0) + 1);
154+
}
155+
for (const w of words2) {
156+
cnt2.set(w, (cnt2.get(w) ?? 0) + 1);
157+
}
158+
let ans = 0;
159+
for (const [w, v] of cnt1) {
160+
if (v === 1 && cnt2.get(w) === 1) {
161+
++ans;
162+
}
163+
}
164+
return ans;
140165
}
141166
```
142167

solution/2000-2099/2085.Count Common Words With One Occurrence/README_EN.md

+52-21
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ Thus, there are 2 strings that appear exactly once in each of the two arrays.
4747

4848
## Solutions
4949

50+
**Solution 1: Hash Table + Counting**
51+
52+
We can use two hash tables, $cnt1$ and $cnt2$, to count the occurrences of each string in the two string arrays respectively. Then, we traverse one of the hash tables. If a string appears once in the other hash table and also appears once in the current hash table, we increment the answer by one.
53+
54+
The time complexity is $O(n + m)$, and the space complexity is $O(n + m)$. Where $n$ and $m$ are the lengths of the two string arrays respectively.
55+
5056
<!-- tabs:start -->
5157

5258
### **Python3**
@@ -56,32 +62,30 @@ class Solution:
5662
def countWords(self, words1: List[str], words2: List[str]) -> int:
5763
cnt1 = Counter(words1)
5864
cnt2 = Counter(words2)
59-
return sum(cnt2[k] == 1 for k, v in cnt1.items() if v == 1)
65+
return sum(v == 1 and cnt2[w] == 1 for w, v in cnt1.items())
6066
```
6167

6268
### **Java**
6369

6470
```java
6571
class Solution {
6672
public int countWords(String[] words1, String[] words2) {
67-
Map<String, Integer> cnt1 = count(words1);
68-
Map<String, Integer> cnt2 = count(words2);
73+
Map<String, Integer> cnt1 = new HashMap<>();
74+
Map<String, Integer> cnt2 = new HashMap<>();
75+
for (var w : words1) {
76+
cnt1.merge(w, 1, Integer::sum);
77+
}
78+
for (var w : words2) {
79+
cnt2.merge(w, 1, Integer::sum);
80+
}
6981
int ans = 0;
70-
for (String w : words1) {
71-
if (cnt1.getOrDefault(w, 0) == 1 && cnt2.getOrDefault(w, 0) == 1) {
82+
for (var e : cnt1.entrySet()) {
83+
if (e.getValue() == 1 && cnt2.getOrDefault(e.getKey(), 0) == 1) {
7284
++ans;
7385
}
7486
}
7587
return ans;
7688
}
77-
78-
private Map<String, Integer> count(String[] words) {
79-
Map<String, Integer> cnt = new HashMap<>();
80-
for (String w : words) {
81-
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
82-
}
83-
return cnt;
84-
}
8589
}
8690
```
8791

@@ -93,10 +97,16 @@ public:
9397
int countWords(vector<string>& words1, vector<string>& words2) {
9498
unordered_map<string, int> cnt1;
9599
unordered_map<string, int> cnt2;
96-
for (auto& w : words1) cnt1[w]++;
97-
for (auto& w : words2) cnt2[w]++;
100+
for (auto& w : words1) {
101+
++cnt1[w];
102+
}
103+
for (auto& w : words2) {
104+
++cnt2[w];
105+
}
98106
int ans = 0;
99-
for (auto& w : words1) ans += (cnt1[w] == 1 && cnt2[w] == 1);
107+
for (auto& [w, v] : cnt1) {
108+
ans += v == 1 && cnt2[w] == 1;
109+
}
100110
return ans;
101111
}
102112
};
@@ -105,7 +115,7 @@ public:
105115
### **Go**
106116
107117
```go
108-
func countWords(words1 []string, words2 []string) int {
118+
func countWords(words1 []string, words2 []string) (ans int) {
109119
cnt1 := map[string]int{}
110120
cnt2 := map[string]int{}
111121
for _, w := range words1 {
@@ -114,13 +124,34 @@ func countWords(words1 []string, words2 []string) int {
114124
for _, w := range words2 {
115125
cnt2[w]++
116126
}
117-
ans := 0
118-
for _, w := range words1 {
119-
if cnt1[w] == 1 && cnt2[w] == 1 {
127+
for w, v := range cnt1 {
128+
if v == 1 && cnt2[w] == 1 {
120129
ans++
121130
}
122131
}
123-
return ans
132+
return
133+
}
134+
```
135+
136+
### **TypeScript**
137+
138+
```ts
139+
function countWords(words1: string[], words2: string[]): number {
140+
const cnt1 = new Map<string, number>();
141+
const cnt2 = new Map<string, number>();
142+
for (const w of words1) {
143+
cnt1.set(w, (cnt1.get(w) ?? 0) + 1);
144+
}
145+
for (const w of words2) {
146+
cnt2.set(w, (cnt2.get(w) ?? 0) + 1);
147+
}
148+
let ans = 0;
149+
for (const [w, v] of cnt1) {
150+
if (v === 1 && cnt2.get(w) === 1) {
151+
++ans;
152+
}
153+
}
154+
return ans;
124155
}
125156
```
126157

solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ class Solution {
33
int countWords(vector<string>& words1, vector<string>& words2) {
44
unordered_map<string, int> cnt1;
55
unordered_map<string, int> cnt2;
6-
for (auto& w : words1) cnt1[w]++;
7-
for (auto& w : words2) cnt2[w]++;
6+
for (auto& w : words1) {
7+
++cnt1[w];
8+
}
9+
for (auto& w : words2) {
10+
++cnt2[w];
11+
}
812
int ans = 0;
9-
for (auto& w : words1) ans += (cnt1[w] == 1 && cnt2[w] == 1);
13+
for (auto& [w, v] : cnt1) {
14+
ans += v == 1 && cnt2[w] == 1;
15+
}
1016
return ans;
1117
}
1218
};
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func countWords(words1 []string, words2 []string) int {
1+
func countWords(words1 []string, words2 []string) (ans int) {
22
cnt1 := map[string]int{}
33
cnt2 := map[string]int{}
44
for _, w := range words1 {
@@ -7,11 +7,10 @@ func countWords(words1 []string, words2 []string) int {
77
for _, w := range words2 {
88
cnt2[w]++
99
}
10-
ans := 0
11-
for _, w := range words1 {
12-
if cnt1[w] == 1 && cnt2[w] == 1 {
10+
for w, v := range cnt1 {
11+
if v == 1 && cnt2[w] == 1 {
1312
ans++
1413
}
1514
}
16-
return ans
15+
return
1716
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
class Solution {
22
public int countWords(String[] words1, String[] words2) {
3-
Map<String, Integer> cnt1 = count(words1);
4-
Map<String, Integer> cnt2 = count(words2);
3+
Map<String, Integer> cnt1 = new HashMap<>();
4+
Map<String, Integer> cnt2 = new HashMap<>();
5+
for (var w : words1) {
6+
cnt1.merge(w, 1, Integer::sum);
7+
}
8+
for (var w : words2) {
9+
cnt2.merge(w, 1, Integer::sum);
10+
}
511
int ans = 0;
6-
for (String w : words1) {
7-
if (cnt1.getOrDefault(w, 0) == 1 && cnt2.getOrDefault(w, 0) == 1) {
12+
for (var e : cnt1.entrySet()) {
13+
if (e.getValue() == 1 && cnt2.getOrDefault(e.getKey(), 0) == 1) {
814
++ans;
915
}
1016
}
1117
return ans;
1218
}
13-
14-
private Map<String, Integer> count(String[] words) {
15-
Map<String, Integer> cnt = new HashMap<>();
16-
for (String w : words) {
17-
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
18-
}
19-
return cnt;
20-
}
2119
}

solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ class Solution:
22
def countWords(self, words1: List[str], words2: List[str]) -> int:
33
cnt1 = Counter(words1)
44
cnt2 = Counter(words2)
5-
return sum(cnt2[k] == 1 for k, v in cnt1.items() if v == 1)
5+
return sum(v == 1 and cnt2[w] == 1 for w, v in cnt1.items())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function countWords(words1: string[], words2: string[]): number {
2+
const cnt1 = new Map<string, number>();
3+
const cnt2 = new Map<string, number>();
4+
for (const w of words1) {
5+
cnt1.set(w, (cnt1.get(w) ?? 0) + 1);
6+
}
7+
for (const w of words2) {
8+
cnt2.set(w, (cnt2.get(w) ?? 0) + 1);
9+
}
10+
let ans = 0;
11+
for (const [w, v] of cnt1) {
12+
if (v === 1 && cnt2.get(w) === 1) {
13+
++ans;
14+
}
15+
}
16+
return ans;
17+
}

0 commit comments

Comments
 (0)