-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2085-CountCommonWordsWithOneOccurrence.go
More file actions
71 lines (63 loc) · 2.99 KB
/
2085-CountCommonWordsWithOneOccurrence.go
File metadata and controls
71 lines (63 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
// 2085. Count Common Words With One Occurrence
// Given two string arrays words1 and words2,
// return the number of strings that appear exactly once in each of the two arrays.
// Example 1:
// Input: words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"]
// Output: 2
// Explanation:
// - "leetcode" appears exactly once in each of the two arrays. We count this string.
// - "amazing" appears exactly once in each of the two arrays. We count this string.
// - "is" appears in each of the two arrays, but there are 2 occurrences of it in words1. We do not count this string.
// - "as" appears once in words1, but does not appear in words2. We do not count this string.
// Thus, there are 2 strings that appear exactly once in each of the two arrays.
// Example 2:
// Input: words1 = ["b","bb","bbb"], words2 = ["a","aa","aaa"]
// Output: 0
// Explanation: There are no strings that appear in each of the two arrays.
// Example 3:
// Input: words1 = ["a","ab"], words2 = ["a","a","a","ab"]
// Output: 1
// Explanation: The only string that appears exactly once in each of the two arrays is "ab".
// Constraints:
// 1 <= words1.length, words2.length <= 1000
// 1 <= words1[i].length, words2[j].length <= 30
// words1[i] and words2[j] consists only of lowercase English letters.
import "fmt"
func countWords(words1 []string, words2 []string) int {
res, mp1, mp2 := 0, make(map[string]int), make(map[string]int)
for _, v := range words1 {
mp1[v]++
}
for _, v := range words2 {
mp2[v]++
}
for k, c := range mp1 {
if c == 1 && mp2[k] == 1 { // 两个字符串数组中 都恰好出现一次
res++
}
}
return res
}
func main() {
// Example 1:
// Input: words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"]
// Output: 2
// Explanation:
// - "leetcode" appears exactly once in each of the two arrays. We count this string.
// - "amazing" appears exactly once in each of the two arrays. We count this string.
// - "is" appears in each of the two arrays, but there are 2 occurrences of it in words1. We do not count this string.
// - "as" appears once in words1, but does not appear in words2. We do not count this string.
// Thus, there are 2 strings that appear exactly once in each of the two arrays.
fmt.Println(countWords([]string{"leetcode","is","amazing","as","is"}, []string{"amazing","leetcode","is"})) // 2
// Example 2:
// Input: words1 = ["b","bb","bbb"], words2 = ["a","aa","aaa"]
// Output: 0
// Explanation: There are no strings that appear in each of the two arrays.
fmt.Println(countWords([]string{"b","bb","bbb"}, []string{"a","aa","aaa"})) // 0
// Example 3:
// Input: words1 = ["a","ab"], words2 = ["a","a","a","ab"]
// Output: 1
// Explanation: The only string that appears exactly once in each of the two arrays is "ab".
fmt.Println(countWords([]string{"a","ab"}, []string{"a","a","a","ab"})) // 1
}