Skip to content

Commit 64a143b

Browse files
committed
feat: add solutions to lc problem: No.2085
No.2085.Count Common Words With One Occurrence
1 parent 85c5909 commit 64a143b

File tree

9 files changed

+196
-14
lines changed

9 files changed

+196
-14
lines changed

solution/0800-0899/0890.Find and Replace Pattern/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43+
**方法一:哈希表**
44+
4345
<!-- tabs:start -->
4446

4547
### **Python3**
@@ -51,13 +53,12 @@ class Solution:
5153
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
5254
def match(s, t):
5355
m1, m2 = [0] * 128, [0] * 128
54-
for i in range(n):
55-
if m1[ord(s[i])] != m2[ord(t[i])]:
56+
for i, (a, b) in enumerate(zip(s, t), 1):
57+
if m1[ord(a)] != m2[ord(b)]:
5658
return False
57-
m1[ord(s[i])] = m2[ord(t[i])] = i + 1
59+
m1[ord(a)] = m2[ord(b)] = i
5860
return True
5961

60-
n = len(pattern)
6162
return [word for word in words if match(word, pattern)]
6263
```
6364

solution/0800-0899/0890.Find and Replace Pattern/README_EN.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ class Solution:
4848
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
4949
def match(s, t):
5050
m1, m2 = [0] * 128, [0] * 128
51-
for i in range(n):
52-
if m1[ord(s[i])] != m2[ord(t[i])]:
51+
for i, (a, b) in enumerate(zip(s, t), 1):
52+
if m1[ord(a)] != m2[ord(b)]:
5353
return False
54-
m1[ord(s[i])] = m2[ord(t[i])] = i + 1
54+
m1[ord(a)] = m2[ord(b)] = i
5555
return True
5656

57-
n = len(pattern)
5857
return [word for word in words if match(word, pattern)]
5958
```
6059

solution/0800-0899/0890.Find and Replace Pattern/Solution.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ class Solution:
22
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
33
def match(s, t):
44
m1, m2 = [0] * 128, [0] * 128
5-
for i in range(n):
6-
if m1[ord(s[i])] != m2[ord(t[i])]:
5+
for i, (a, b) in enumerate(zip(s, t), 1):
6+
if m1[ord(a)] != m2[ord(b)]:
77
return False
8-
m1[ord(s[i])] = m2[ord(t[i])] = i + 1
8+
m1[ord(a)] = m2[ord(b)] = i
99
return True
1010

11-
n = len(pattern)
1211
return [word for word in words if match(word, pattern)]

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,87 @@
5353

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

56+
**方法一:哈希表计数**
57+
5658
<!-- tabs:start -->
5759

5860
### **Python3**
5961

6062
<!-- 这里可写当前语言的特殊实现逻辑 -->
6163

6264
```python
63-
65+
class Solution:
66+
def countWords(self, words1: List[str], words2: List[str]) -> int:
67+
cnt1 = Counter(words1)
68+
cnt2 = Counter(words2)
69+
return sum(cnt2[k] == 1 for k, v in cnt1.items() if v == 1)
6470
```
6571

6672
### **Java**
6773

6874
<!-- 这里可写当前语言的特殊实现逻辑 -->
6975

7076
```java
77+
class Solution {
78+
public int countWords(String[] words1, String[] words2) {
79+
Map<String, Integer> cnt1 = count(words1);
80+
Map<String, Integer> cnt2 = count(words2);
81+
int ans = 0;
82+
for (String w : words1) {
83+
if (cnt1.getOrDefault(w, 0) == 1 && cnt2.getOrDefault(w, 0) == 1) {
84+
++ans;
85+
}
86+
}
87+
return ans;
88+
}
89+
90+
private Map<String, Integer> count(String[] words) {
91+
Map<String, Integer> cnt = new HashMap<>();
92+
for (String w : words) {
93+
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
94+
}
95+
return cnt;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int countWords(vector<string>& words1, vector<string>& words2) {
106+
unordered_map<string, int> cnt1;
107+
unordered_map<string, int> cnt2;
108+
for (auto& w : words1) cnt1[w]++;
109+
for (auto& w : words2) cnt2[w]++;
110+
int ans = 0;
111+
for (auto& w : words1) ans += (cnt1[w] == 1 && cnt2[w] == 1);
112+
return ans;
113+
}
114+
};
115+
```
71116
117+
### **Go**
118+
119+
```go
120+
func countWords(words1 []string, words2 []string) int {
121+
cnt1 := map[string]int{}
122+
cnt2 := map[string]int{}
123+
for _, w := range words1 {
124+
cnt1[w]++
125+
}
126+
for _, w := range words2 {
127+
cnt2[w]++
128+
}
129+
ans := 0
130+
for _, w := range words1 {
131+
if cnt1[w] == 1 && cnt2[w] == 1 {
132+
ans++
133+
}
134+
}
135+
return ans
136+
}
72137
```
73138

74139
### **...**

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,76 @@ Thus, there are 2 strings that appear exactly once in each of the two arrays.
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def countWords(self, words1: List[str], words2: List[str]) -> int:
57+
cnt1 = Counter(words1)
58+
cnt2 = Counter(words2)
59+
return sum(cnt2[k] == 1 for k, v in cnt1.items() if v == 1)
5660
```
5761

5862
### **Java**
5963

6064
```java
65+
class Solution {
66+
public int countWords(String[] words1, String[] words2) {
67+
Map<String, Integer> cnt1 = count(words1);
68+
Map<String, Integer> cnt2 = count(words2);
69+
int ans = 0;
70+
for (String w : words1) {
71+
if (cnt1.getOrDefault(w, 0) == 1 && cnt2.getOrDefault(w, 0) == 1) {
72+
++ans;
73+
}
74+
}
75+
return ans;
76+
}
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+
}
85+
}
86+
```
87+
88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
int countWords(vector<string>& words1, vector<string>& words2) {
94+
unordered_map<string, int> cnt1;
95+
unordered_map<string, int> cnt2;
96+
for (auto& w : words1) cnt1[w]++;
97+
for (auto& w : words2) cnt2[w]++;
98+
int ans = 0;
99+
for (auto& w : words1) ans += (cnt1[w] == 1 && cnt2[w] == 1);
100+
return ans;
101+
}
102+
};
103+
```
61104
105+
### **Go**
106+
107+
```go
108+
func countWords(words1 []string, words2 []string) int {
109+
cnt1 := map[string]int{}
110+
cnt2 := map[string]int{}
111+
for _, w := range words1 {
112+
cnt1[w]++
113+
}
114+
for _, w := range words2 {
115+
cnt2[w]++
116+
}
117+
ans := 0
118+
for _, w := range words1 {
119+
if cnt1[w] == 1 && cnt2[w] == 1 {
120+
ans++
121+
}
122+
}
123+
return ans
124+
}
62125
```
63126

64127
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int countWords(vector<string>& words1, vector<string>& words2) {
4+
unordered_map<string, int> cnt1;
5+
unordered_map<string, int> cnt2;
6+
for (auto& w : words1) cnt1[w]++;
7+
for (auto& w : words2) cnt2[w]++;
8+
int ans = 0;
9+
for (auto& w : words1) ans += (cnt1[w] == 1 && cnt2[w] == 1);
10+
return ans;
11+
}
12+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func countWords(words1 []string, words2 []string) int {
2+
cnt1 := map[string]int{}
3+
cnt2 := map[string]int{}
4+
for _, w := range words1 {
5+
cnt1[w]++
6+
}
7+
for _, w := range words2 {
8+
cnt2[w]++
9+
}
10+
ans := 0
11+
for _, w := range words1 {
12+
if cnt1[w] == 1 && cnt2[w] == 1 {
13+
ans++
14+
}
15+
}
16+
return ans
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int countWords(String[] words1, String[] words2) {
3+
Map<String, Integer> cnt1 = count(words1);
4+
Map<String, Integer> cnt2 = count(words2);
5+
int ans = 0;
6+
for (String w : words1) {
7+
if (cnt1.getOrDefault(w, 0) == 1 && cnt2.getOrDefault(w, 0) == 1) {
8+
++ans;
9+
}
10+
}
11+
return ans;
12+
}
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+
}
21+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def countWords(self, words1: List[str], words2: List[str]) -> int:
3+
cnt1 = Counter(words1)
4+
cnt2 = Counter(words2)
5+
return sum(cnt2[k] == 1 for k, v in cnt1.items() if v == 1)

0 commit comments

Comments
 (0)