-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path26. Word Subsets
More file actions
35 lines (27 loc) · 875 Bytes
/
26. Word Subsets
File metadata and controls
35 lines (27 loc) · 875 Bytes
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
class Solution {
public List<String> wordSubsets(String[] A, String[] B) {
List<String> result = new ArrayList<>();
int[] target = new int[26];
for(String b:B){
int[] temp=new int[26];
for(char c:b.toCharArray()){
temp[c-'a']++;
target[c-'a'] = Math.max(target[c-'a'],temp[c-'a']);
}
}
for(String a:A){
int[] arr=new int[26];
for(char c:a.toCharArray()) arr[c-'a']++;
if(subset(arr,target)){
result.add(a);
}
}
return result;
}
private boolean subset(int[] source,int[] dest){
for(int i=0;i<26;i++){
if(dest[i]>source[i]) return false;
}
return true;
}
}