-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathWord Subsets.java
39 lines (39 loc) · 890 Bytes
/
Word Subsets.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
38
39
class Solution {
public List<String> wordSubsets(String[] words1, String[] words2) {
List<String> list=new ArrayList<>();
int[] bmax=count("");
for(String w2:words2)
{
int[] b=count(w2);
for(int i=0;i<26;i++)
{
bmax[i]=Math.max(bmax[i],b[i]);
}
}
for(String w1:words1)
{
int[] a=count(w1);
for(int i=0;i<26;i++)
{
if(a[i]<bmax[i])
{
break;
}
if(i==25)
{
list.add(w1);
}
}
}
return list;
}
public int[] count(String s)
{
int[] ans=new int[26];
for(char c:s.toCharArray())
{
ans[c-'a']++;
}
return ans;
}
}