-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSubset916.java
More file actions
36 lines (35 loc) · 1.56 KB
/
WordSubset916.java
File metadata and controls
36 lines (35 loc) · 1.56 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
class Solution {
// Brute Force Approach-> ek list bnate uske andr string or hashmap bnate hrr string ka or frequency count krte fir word2 ke hrr string se check krwate ki valid hai ya nahi hai
// Iss approach mein hmne atleast frequency ki ek array bnai mtlb ki itne toh hone hi chahiye character order matter nahi kryta hai uske alava baari baari hrr ek word ki frequency se maxfrequency nikali or word1 array ke andr check kia ki uske temp ki value agr required max frequency ke requirement se small hai tb toh nahi kr paega vo universal vrna kr jaega
public List<String> wordSubsets(String[] words1, String[] words2) {
int[] maxFrequency=new int[26];
for(String str:words2){
int[] temp=new int[26];
for(int i=0;i<str.length();i++){
temp[str.charAt(i)-'a']++;
}
for(int i=0;i<maxFrequency.length;i++){
maxFrequency[i]=Math.max(temp[i],maxFrequency[i]);
}
}
List<String> ans=new ArrayList<>();
for(String str:words1){
String wordRequired=str;
int[] temp=new int[26];
for(int i=0;i<wordRequired.length();i++){
temp[wordRequired.charAt(i)-'a']++;
}
boolean haiYanai=true;
for(int i=0;i<temp.length;i++){
if(temp[i]<maxFrequency[i]){
haiYanai=false;
break;
}
}
if(haiYanai){
ans.add(wordRequired);
}
}
return ans;
}
}