-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path916.java
37 lines (30 loc) · 1.08 KB
/
916.java
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
class Solution {
public List<String> wordSubsets(String[] universalSet, String[] subsetWords) {
int[] maxSubsetFreq = new int[26];
for (String subsetWord : subsetWords) {
int[] tempFreq = new int[26];
for (char ch : subsetWord.toCharArray()) {
tempFreq[ch - 'a']++;
maxSubsetFreq[ch - 'a'] = Math.max(maxSubsetFreq[ch - 'a'], tempFreq[ch - 'a']);
}
}
List<String> result = new ArrayList<>();
for (String word : universalSet) {
int[] wordFreq = new int[26];
for (char ch : word.toCharArray()) {
wordFreq[ch - 'a']++;
}
boolean isUniversal = true;
for (int i = 0; i < 26; ++i) {
if (maxSubsetFreq[i] > wordFreq[i]) {
isUniversal = false;
break;
}
}
if (isUniversal) {
result.add(word);
}
}
return result;
}
}