-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount-the-number-of-consistent-strings.java
48 lines (44 loc) · 1.2 KB
/
count-the-number-of-consistent-strings.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
40
41
42
43
44
45
46
47
48
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
boolean[] s=new boolean[26];
for(char c:allowed.toCharArray()){
s[c-'a']=true;
}
int count=0;
for(String w:words){
if(check(w,s)){
count++;
}
}
return count;
}
private boolean check(String w,boolean[] s){
for(int i=0;i<w.length();i++){
if(!s[w.charAt(i)-'a']){
return false;
}
}
return true;
}
}
//OR LESS OPTIMIZED
// class Solution {
// public int countConsistentStrings(String allowed, String[] words) {
// Set<Character> set=new HashSet<>();
// for(int i=0;i<allowed.length();i++){
// set.add(allowed.charAt(i));
// }
// int count=0;
// for(int i=0;i<words.length;i++){
// int flag=1;
// for(int j=0;j<words[i].length();j++){
// if(!set.contains(words[i].charAt(j))){
// flag=0;
// break;
// }
// }
// count+=flag;
// }
// return count;
// }
// }