-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
36 lines (32 loc) · 938 Bytes
/
Solution.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
class Solution {
public List<String> wordSubsets(String[] A, String[] B) {
int[] bmax = frequency("");
for (String s : B) {
int[] count = frequency(s);
for (int i = 0; i < 26; i++) {
bmax[i] = Math.max(bmax[i], count[i]);
}
}
List<String> res = new ArrayList<>();
for (String s : A) {
int[] count = frequency(s);
boolean found = true;
for (int i = 0; i < 26; i++) {
if (bmax[i] > count[i]) {
found = false;
break;
}
}
if (found)
res.add(s);
}
return res;
}
private int[] frequency(String s) {
int[] count = new int[26];
for (char c : s.toCharArray()) {
count[c - 'a']++;
}
return count;
}
}