| 1 | class Solution { |
| 2 | public List<String> wordSubsets(String[] words1, String[] words2) { |
| 3 | int freq[] = new int[26]; |
| 4 | for(String word : words2){ //M |
| 5 | int temp[] = getFreq(word); //p |
| 6 | for(int i=0;i<26;i++){ //26 |
| 7 | freq[i] = Math.max(freq[i], temp[i]); |
| 8 | } |
| 9 | } |
| 10 | List<String> res = new ArrayList<>(); |
| 11 | for(String word : words1){ //n |
| 12 | int temp[] = getFreq(word); //p |
| 13 | boolean flag=true; |
| 14 | for(int i=0;i<26;i++){ //26 |
| 15 | if(freq[i] > temp[i]){ |
| 16 | flag = false; |
| 17 | break; |
| 18 | } |
| 19 | } |
| 20 | if(flag){ |
| 21 | res.add(word); |
| 22 | } |
| 23 | } |
| 24 | return res; |
| 25 | } |
| 26 | public int[] getFreq(String word){ |
| 27 | int freq[] = new int[26]; |
| 28 | for(int i=0;i<word.length();i++){ |