Skip to content

feat: add solutions to lc problem: No.2085 #2209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@

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

**方法一:哈希表**
**方法一:哈希表计数**

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

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

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

### **Java**
Expand All @@ -80,24 +80,22 @@ class Solution:
```java
class Solution {
public int countWords(String[] words1, String[] words2) {
Map<String, Integer> cnt1 = count(words1);
Map<String, Integer> cnt2 = count(words2);
Map<String, Integer> cnt1 = new HashMap<>();
Map<String, Integer> cnt2 = new HashMap<>();
for (var w : words1) {
cnt1.merge(w, 1, Integer::sum);
}
for (var w : words2) {
cnt2.merge(w, 1, Integer::sum);
}
int ans = 0;
for (String w : words1) {
if (cnt1.getOrDefault(w, 0) == 1 && cnt2.getOrDefault(w, 0) == 1) {
for (var e : cnt1.entrySet()) {
if (e.getValue() == 1 && cnt2.getOrDefault(e.getKey(), 0) == 1) {
++ans;
}
}
return ans;
}

private Map<String, Integer> count(String[] words) {
Map<String, Integer> cnt = new HashMap<>();
for (String w : words) {
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
}
return cnt;
}
}
```

Expand All @@ -109,10 +107,16 @@ public:
int countWords(vector<string>& words1, vector<string>& words2) {
unordered_map<string, int> cnt1;
unordered_map<string, int> cnt2;
for (auto& w : words1) cnt1[w]++;
for (auto& w : words2) cnt2[w]++;
for (auto& w : words1) {
++cnt1[w];
}
for (auto& w : words2) {
++cnt2[w];
}
int ans = 0;
for (auto& w : words1) ans += (cnt1[w] == 1 && cnt2[w] == 1);
for (auto& [w, v] : cnt1) {
ans += v == 1 && cnt2[w] == 1;
}
return ans;
}
};
Expand All @@ -121,7 +125,7 @@ public:
### **Go**

```go
func countWords(words1 []string, words2 []string) int {
func countWords(words1 []string, words2 []string) (ans int) {
cnt1 := map[string]int{}
cnt2 := map[string]int{}
for _, w := range words1 {
Expand All @@ -130,13 +134,34 @@ func countWords(words1 []string, words2 []string) int {
for _, w := range words2 {
cnt2[w]++
}
ans := 0
for _, w := range words1 {
if cnt1[w] == 1 && cnt2[w] == 1 {
for w, v := range cnt1 {
if v == 1 && cnt2[w] == 1 {
ans++
}
}
return ans
return
}
```

### **TypeScript**

```ts
function countWords(words1: string[], words2: string[]): number {
const cnt1 = new Map<string, number>();
const cnt2 = new Map<string, number>();
for (const w of words1) {
cnt1.set(w, (cnt1.get(w) ?? 0) + 1);
}
for (const w of words2) {
cnt2.set(w, (cnt2.get(w) ?? 0) + 1);
}
let ans = 0;
for (const [w, v] of cnt1) {
if (v === 1 && cnt2.get(w) === 1) {
++ans;
}
}
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ Thus, there are 2 strings that appear exactly once in each of the two arrays.

## Solutions

**Solution 1: Hash Table + Counting**

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.

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.

<!-- tabs:start -->

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

### **Java**

```java
class Solution {
public int countWords(String[] words1, String[] words2) {
Map<String, Integer> cnt1 = count(words1);
Map<String, Integer> cnt2 = count(words2);
Map<String, Integer> cnt1 = new HashMap<>();
Map<String, Integer> cnt2 = new HashMap<>();
for (var w : words1) {
cnt1.merge(w, 1, Integer::sum);
}
for (var w : words2) {
cnt2.merge(w, 1, Integer::sum);
}
int ans = 0;
for (String w : words1) {
if (cnt1.getOrDefault(w, 0) == 1 && cnt2.getOrDefault(w, 0) == 1) {
for (var e : cnt1.entrySet()) {
if (e.getValue() == 1 && cnt2.getOrDefault(e.getKey(), 0) == 1) {
++ans;
}
}
return ans;
}

private Map<String, Integer> count(String[] words) {
Map<String, Integer> cnt = new HashMap<>();
for (String w : words) {
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
}
return cnt;
}
}
```

Expand All @@ -93,10 +97,16 @@ public:
int countWords(vector<string>& words1, vector<string>& words2) {
unordered_map<string, int> cnt1;
unordered_map<string, int> cnt2;
for (auto& w : words1) cnt1[w]++;
for (auto& w : words2) cnt2[w]++;
for (auto& w : words1) {
++cnt1[w];
}
for (auto& w : words2) {
++cnt2[w];
}
int ans = 0;
for (auto& w : words1) ans += (cnt1[w] == 1 && cnt2[w] == 1);
for (auto& [w, v] : cnt1) {
ans += v == 1 && cnt2[w] == 1;
}
return ans;
}
};
Expand All @@ -105,7 +115,7 @@ public:
### **Go**

```go
func countWords(words1 []string, words2 []string) int {
func countWords(words1 []string, words2 []string) (ans int) {
cnt1 := map[string]int{}
cnt2 := map[string]int{}
for _, w := range words1 {
Expand All @@ -114,13 +124,34 @@ func countWords(words1 []string, words2 []string) int {
for _, w := range words2 {
cnt2[w]++
}
ans := 0
for _, w := range words1 {
if cnt1[w] == 1 && cnt2[w] == 1 {
for w, v := range cnt1 {
if v == 1 && cnt2[w] == 1 {
ans++
}
}
return ans
return
}
```

### **TypeScript**

```ts
function countWords(words1: string[], words2: string[]): number {
const cnt1 = new Map<string, number>();
const cnt2 = new Map<string, number>();
for (const w of words1) {
cnt1.set(w, (cnt1.get(w) ?? 0) + 1);
}
for (const w of words2) {
cnt2.set(w, (cnt2.get(w) ?? 0) + 1);
}
let ans = 0;
for (const [w, v] of cnt1) {
if (v === 1 && cnt2.get(w) === 1) {
++ans;
}
}
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ class Solution {
int countWords(vector<string>& words1, vector<string>& words2) {
unordered_map<string, int> cnt1;
unordered_map<string, int> cnt2;
for (auto& w : words1) cnt1[w]++;
for (auto& w : words2) cnt2[w]++;
for (auto& w : words1) {
++cnt1[w];
}
for (auto& w : words2) {
++cnt2[w];
}
int ans = 0;
for (auto& w : words1) ans += (cnt1[w] == 1 && cnt2[w] == 1);
for (auto& [w, v] : cnt1) {
ans += v == 1 && cnt2[w] == 1;
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func countWords(words1 []string, words2 []string) int {
func countWords(words1 []string, words2 []string) (ans int) {
cnt1 := map[string]int{}
cnt2 := map[string]int{}
for _, w := range words1 {
Expand All @@ -7,11 +7,10 @@ func countWords(words1 []string, words2 []string) int {
for _, w := range words2 {
cnt2[w]++
}
ans := 0
for _, w := range words1 {
if cnt1[w] == 1 && cnt2[w] == 1 {
for w, v := range cnt1 {
if v == 1 && cnt2[w] == 1 {
ans++
}
}
return ans
return
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
class Solution {
public int countWords(String[] words1, String[] words2) {
Map<String, Integer> cnt1 = count(words1);
Map<String, Integer> cnt2 = count(words2);
Map<String, Integer> cnt1 = new HashMap<>();
Map<String, Integer> cnt2 = new HashMap<>();
for (var w : words1) {
cnt1.merge(w, 1, Integer::sum);
}
for (var w : words2) {
cnt2.merge(w, 1, Integer::sum);
}
int ans = 0;
for (String w : words1) {
if (cnt1.getOrDefault(w, 0) == 1 && cnt2.getOrDefault(w, 0) == 1) {
for (var e : cnt1.entrySet()) {
if (e.getValue() == 1 && cnt2.getOrDefault(e.getKey(), 0) == 1) {
++ans;
}
}
return ans;
}

private Map<String, Integer> count(String[] words) {
Map<String, Integer> cnt = new HashMap<>();
for (String w : words) {
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
}
return cnt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
cnt1 = Counter(words1)
cnt2 = Counter(words2)
return sum(cnt2[k] == 1 for k, v in cnt1.items() if v == 1)
return sum(v == 1 and cnt2[w] == 1 for w, v in cnt1.items())
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function countWords(words1: string[], words2: string[]): number {
const cnt1 = new Map<string, number>();
const cnt2 = new Map<string, number>();
for (const w of words1) {
cnt1.set(w, (cnt1.get(w) ?? 0) + 1);
}
for (const w of words2) {
cnt2.set(w, (cnt2.get(w) ?? 0) + 1);
}
let ans = 0;
for (const [w, v] of cnt1) {
if (v === 1 && cnt2.get(w) === 1) {
++ans;
}
}
return ans;
}