-
Notifications
You must be signed in to change notification settings - Fork 0
/
1684. Count the Number of Consistent Strings
53 lines (41 loc) · 1.2 KB
/
1684. Count the Number of Consistent Strings
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
49
50
51
52
53
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
int count=words.length;
HashSet<Character> set=new HashSet<>();
for(char letter: allowed.toCharArray()){
set.add(letter);
}
for(String word: words){
for(int i=0;i<word.length();i++){
if(!set.contains(word.charAt(i))){
count--;
break;
}
}
}
return count;
}
}
//Approach-2 (using boolean array)
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
boolean[] isAllowed = new boolean[26];
for (char ch : allowed.toCharArray()) {
isAllowed[ch - 'a'] = true;
}
int count = 0;
for (String word : words) {
boolean allCharAllowed = true;
for (char ch : word.toCharArray()) {
if (!isAllowed[ch - 'a']) {
allCharAllowed = false;
break;
}
}
if (allCharAllowed) {
count++;
}
}
return count;
}
}