-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0916.go
77 lines (67 loc) · 1.98 KB
/
0916.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
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
72
73
74
75
76
77
// Source: https://leetcode.com/problems/word-subsets
// Title: Word Subsets
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given two string arrays words1 and words2.
//
// A string b is a subset of string a if every letter in b occurs in a including multiplicity.
//
// For example, "wrr" is a subset of "warrior" but is not a subset of "world".
//
// A string a from words1 is universal if for every string b in words2, b is a subset of a.
//
// Return an array of all the universal strings in words1. You may return the answer in any order.
//
// Example 1:
//
// Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"]
// Output: ["facebook","google","leetcode"]
//
// Example 2:
//
// Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"]
// Output: ["apple","google","leetcode"]
//
// Constraints:
//
// 1 <= words1.length, words2.length <= 10^4
// 1 <= words1[i].length, words2[i].length <= 10
// words1[i] and words2[i] consist only of lowercase English letters.
// All the strings of words1 are unique.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
func wordSubsets(words1 []string, words2 []string) []string {
countAll := make([]int, 26)
for _, word := range words2 {
count := make([]int, 26)
for _, ch := range word {
count[ch-'a']++
}
for ch, num := range count {
countAll[ch] = _max(countAll[ch], num)
}
}
res := make([]string, 0, len(words1))
OUTER:
for _, word := range words1 {
count := make([]int, 26)
for _, ch := range word {
count[ch-'a']++
}
for ch, num := range countAll {
if count[ch] < num {
continue OUTER
}
}
res = append(res, word)
}
return res
}
func _max(a, b int) int {
if a > b {
return a
}
return b
}