diff --git a/solution/2000-2099/2085.Count Common Words With One Occurrence/README.md b/solution/2000-2099/2085.Count Common Words With One Occurrence/README.md index 87fefb3279a72..6803e9dc5b365 100644 --- a/solution/2000-2099/2085.Count Common Words With One Occurrence/README.md +++ b/solution/2000-2099/2085.Count Common Words With One Occurrence/README.md @@ -53,9 +53,9 @@ -**方法一:哈希表** +**方法一:哈希表计数** -我们可以用两个哈希表分别统计两个字符串数组中每个字符串出现的次数,然后遍历其中一个哈希表,如果某个字符串在另一个哈希表中出现了一次,且在当前哈希表中也出现了一次,则答案加一。 +我们可以用两个哈希表 $cnt1$ 和 $cnt2$ 分别统计两个字符串数组中每个字符串出现的次数,然后遍历其中一个哈希表,如果某个字符串在另一个哈希表中出现了一次,且在当前哈希表中也出现了一次,则答案加一。 时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是两个字符串数组的长度。 @@ -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** @@ -80,24 +80,22 @@ class Solution: ```java class Solution { public int countWords(String[] words1, String[] words2) { - Map cnt1 = count(words1); - Map cnt2 = count(words2); + Map cnt1 = new HashMap<>(); + Map 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 count(String[] words) { - Map cnt = new HashMap<>(); - for (String w : words) { - cnt.put(w, cnt.getOrDefault(w, 0) + 1); - } - return cnt; - } } ``` @@ -109,10 +107,16 @@ public: int countWords(vector& words1, vector& words2) { unordered_map cnt1; unordered_map 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; } }; @@ -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 { @@ -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(); + const cnt2 = new Map(); + 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; } ``` diff --git a/solution/2000-2099/2085.Count Common Words With One Occurrence/README_EN.md b/solution/2000-2099/2085.Count Common Words With One Occurrence/README_EN.md index 8c00227000d18..010e07460e322 100644 --- a/solution/2000-2099/2085.Count Common Words With One Occurrence/README_EN.md +++ b/solution/2000-2099/2085.Count Common Words With One Occurrence/README_EN.md @@ -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. + ### **Python3** @@ -56,7 +62,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** @@ -64,24 +70,22 @@ class Solution: ```java class Solution { public int countWords(String[] words1, String[] words2) { - Map cnt1 = count(words1); - Map cnt2 = count(words2); + Map cnt1 = new HashMap<>(); + Map 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 count(String[] words) { - Map cnt = new HashMap<>(); - for (String w : words) { - cnt.put(w, cnt.getOrDefault(w, 0) + 1); - } - return cnt; - } } ``` @@ -93,10 +97,16 @@ public: int countWords(vector& words1, vector& words2) { unordered_map cnt1; unordered_map 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; } }; @@ -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 { @@ -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(); + const cnt2 = new Map(); + 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; } ``` diff --git a/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.cpp b/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.cpp index 80f99b6d1e348..a8f931c5a47e9 100644 --- a/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.cpp +++ b/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.cpp @@ -3,10 +3,16 @@ class Solution { int countWords(vector& words1, vector& words2) { unordered_map cnt1; unordered_map 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; } }; \ No newline at end of file diff --git a/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.go b/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.go index 4944125081a4a..abe50fe466a8a 100644 --- a/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.go +++ b/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.go @@ -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 { @@ -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 } \ No newline at end of file diff --git a/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.java b/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.java index 8b2557df393af..5da266de10127 100644 --- a/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.java +++ b/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.java @@ -1,21 +1,19 @@ class Solution { public int countWords(String[] words1, String[] words2) { - Map cnt1 = count(words1); - Map cnt2 = count(words2); + Map cnt1 = new HashMap<>(); + Map 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 count(String[] words) { - Map cnt = new HashMap<>(); - for (String w : words) { - cnt.put(w, cnt.getOrDefault(w, 0) + 1); - } - return cnt; - } } \ No newline at end of file diff --git a/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.py b/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.py index 4fe15942faa54..49f27a8e282c5 100644 --- a/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.py +++ b/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.py @@ -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()) diff --git a/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.ts b/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.ts new file mode 100644 index 0000000000000..a7c19ca0e3899 --- /dev/null +++ b/solution/2000-2099/2085.Count Common Words With One Occurrence/Solution.ts @@ -0,0 +1,17 @@ +function countWords(words1: string[], words2: string[]): number { + const cnt1 = new Map(); + const cnt2 = new Map(); + 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; +}