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
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
boolean[] allowedChars = new boolean[26];
for (int i = 0; i < allowed.length(); i++) {
allowedChars[allowed.charAt(i) - 'a'] = true;
}

int consistentStrings = 0;
for (String word : words) {
if (isConsistentString(word, allowedChars)) {
consistentStrings++;
}
}

return consistentStrings;
}

private boolean isConsistentString(String word, boolean[] allowedChars) {
for (int i = 0; i < word.length(); i++) {
if (!allowedChars[word.charAt(i) - 'a']) {
return false;
}
}

return true;
}
}

References

1684. Count the Number of Consistent Strings