class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
allowed_set = set(allowed)
count = 0
for word in words:
if all(char in allowed_set for char in word):
count += 1
return count
class Solution {
public:
int countConsistentStrings(string allowed, vector<string>& words) {
unordered_set<char> allowedSet(allowed.begin(), allowed.end());
int count = 0;
for (const string& word : words) {
bool consistent = true;
for (char c : word) {
if (allowedSet.find(c) == allowedSet.end()) {
consistent = false;
break;
}
}
if (consistent) count++;
}
return count;
}
};
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
Set<Character> allowedSet = new HashSet<>();
for (char c : allowed.toCharArray()) {
allowedSet.add(c);
}
int count = 0;
for (String word : words) {
boolean consistent = true;
for (char c : word.toCharArray()) {
if (!allowedSet.contains(c)) {
consistent = false;
break;
}
}
if (consistent) count++;
}
return count;
}
}
var countConsistentStrings = function(allowed, words) {
const allowedSet = new Set(allowed);
let count = 0;
for (const word of words) {
let consistent = true;
for (const char of word) {
if (!allowedSet.has(char)) {
consistent = false;
break;
}
}
if (consistent) count++;
}
return count;
};
Given a string allowed
consisting of distinct lowercase English letters, and an array of strings words
, your task is to count how many words in words
are consistent.
A word is considered consistent if every character in the word appears in the allowed
string.
Key constraints:
allowed
can be used any number of times (no reuse restriction).
The core of this problem is to verify, for each word, whether all its letters are present in the allowed
set.
At first, one might think of checking each character of every word against the allowed
string using a loop. However, this approach could be slow if allowed
or the words are long, as checking membership in a string is O(N).
To optimize, we can quickly check if a character is in allowed
by converting allowed
to a set (or hash set/map) for O(1) lookups.
The process is: for each word, check every character. If all are in allowed
, increment the count.
This is a classic case of moving from a brute-force (nested loops, slow lookups) to an optimized approach using a set for fast membership checking.
allowed
string into a set or hash set. This allows us to check if a character is allowed in constant time.words
array:
allowed
set, mark the word as inconsistent and move to the next word.
We use a set for allowed
because:
allowed
.
Example:
allowed = "ab"
words = ["ad","bd","aaab","baa","badab"]
Let's walk through each word:
allowed
(as a string). Each check is O(L), where L is the length of allowed
.allowed
.allowed
to a set: O(L).The optimized approach is much faster, especially for large inputs, because set lookups are constant time.
To solve the "Count the Number of Consistent Strings" problem, we leveraged a set for fast character lookups. This allowed us to efficiently check each word for consistency with the allowed
string. The key insight was to use data structures that offer constant time membership checks, reducing overall complexity and making the solution scalable and elegant.