-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.go
37 lines (34 loc) · 929 Bytes
/
solution.go
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
func wordSubsets(words1 []string, words2 []string) []string {
maxFreq := make([]int, 26)
// Precompute the maximum frequency for each character in words2
for _, word := range words2 {
freq := make([]int, 26)
for _, char := range word {
freq[char-'a']++
}
for i := 0; i < 26; i++ {
if freq[i] > maxFreq[i] {
maxFreq[i] = freq[i]
}
}
}
result := []string{}
// Check each word in words1
for _, word := range words1 {
freq := make([]int, 26)
for _, char := range word {
freq[char-'a']++
}
isUniversal := true
for i := 0; i < 26; i++ {
if freq[i] < maxFreq[i] {
isUniversal = false
break
}
}
if isUniversal {
result = append(result, word)
}
}
return result
}