Leetcode / 1684. Count the Number of Consistent Strings
Pick a programming language:
Here is the source code for the solution to this problem.
class Solution {
// time O(n^2)
// space O(n)
public int countConsistentStrings(String allowed, String[] words) {
HashSet<Character> hashSet = new HashSet<Character>();
int count = 0;
for (int i = 0; i < allowed.length(); i++) {
hashSet.add(allowed.charAt(i));
}
outerloop:
for (int i = 0; i < words.length; i++) {
String word = words[i];
for (int j = 0; j < word.length(); j++) {
if (!hashSet.contains(word.charAt(j))) {
continue outerloop;
}
}
count++;
}
return count;
}
}
Did you like the lesson? 😆👍
Consider a donation to support our work: