-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ916WordSubsets.java
More file actions
46 lines (39 loc) · 1.44 KB
/
Q916WordSubsets.java
File metadata and controls
46 lines (39 loc) · 1.44 KB
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
/*
@b-knd (jingru) on 30 July 2022 08:42:00
*/
class Q916WordSubsets {
public List<String> wordSubsets(String[] words1, String[] words2) {
List<String> res = new ArrayList<>();
int[] target = new int[26];
//build an array of target, that contains the maximum count of characters after looping through all string in words2
for(String a: words2){
int[] temp = new int[26];
for(Character b: a.toCharArray()){
temp[b-'a']++;
target[b-'a'] = Math.max(temp[b-'a'], target[b-'a']);
}
}
//build an array for each word, containing character count
for(String b: words1){
int[] arr = new int[26];
for(Character c: b.toCharArray()){
arr[c-'a']++;
}
if(isSubset(arr, target)){
res.add(b);
}
}
return res;
}
//a word is subset of another if and only if character count is smaller than or equals to character count of the target string
public boolean isSubset(int[] a, int[] b){
for(int i = 0; i < b.length; i++){
if(b[i] > a[i]){
return false;
}
}
return true;
}
}
//Runtime: 31 ms, faster than 73.58% of Java online submissions for Word Subsets.
//Memory Usage: 88.5 MB, less than 55.69% of Java online submissions for Word Subsets.