You are given a string allowed
consisting of distinct characters and an array of strings words
. A string is consistent if all characters in the string appear in the string allowed
.
Return the number of consistent strings in the array words
.
Example 1:
Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"] Output: 2 Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'.
Example 2:
Input: allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"] Output: 7 Explanation: All strings are consistent.
Example 3:
Input: allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"] Output: 4 Explanation: Strings "cc", "acd", "ac", and "d" are consistent.
Constraints:
1 <= words.length <= 104
1 <= allowed.length <= 26
1 <= words[i].length <= 10
allowed
are distinct.words[i]
and allowed
contain only lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem involves checking each word individually against the allowed characters. We want to find how many words only use characters from a given set of approved letters. We will do this by exhaustively checking each word.
Here's how the algorithm would work step-by-step:
def count_consistent_strings_brute_force(allowed_characters, words):
consistent_string_count = 0
for word in words:
is_consistent = True
# Check each letter in the word
for letter in word:
# If the letter isn't allowed, word is inconsistent
if letter not in allowed_characters:
is_consistent = False
break
# Increment the counter if it's a consistent string
if is_consistent:
consistent_string_count += 1
return consistent_string_count
We need to figure out how many words are made up entirely of specific allowed letters. The fastest way is to create a way to quickly check if a word is acceptable, then go through the words and count how many pass the check.
Here's how the algorithm would work step-by-step:
def count_consistent_strings(allowed_characters, words):
allowed_set = set(allowed_characters)
consistent_word_count = 0
# Iterate through each word to check its consistency
for word in words:
is_consistent = True
# Iterate through each character of the current word
for character in word:
#Check if the character exists in the allowed set
if character not in allowed_set:
is_consistent = False
break
# If the word is consistent, increment counter
if is_consistent:
consistent_word_count += 1
# Return the number of consistent strings
return consistent_word_count
Case | How to Handle |
---|---|
allowed is null or empty | Return 0 because no string in words can be consistent without allowed characters. |
words is null or empty | Return 0 because there are no words to check for consistency. |
words contains an empty string | An empty string is considered consistent if allowed is not empty and inconsistent if allowed is empty (already handled above). |
allowed contains an empty string (which is invalid based on the problem description that allowed consists of distinct characters) | If allowed is empty this case should already be handled and the string is inconsistent since no character could be allowed. |
words array contains very long strings | The solution should iterate through each character in the word only once to check if all characters are in allowed, leading to O(n*m) complexity (n is word length and m is words array length). |
allowed string contains all possible characters (e.g., all lowercase letters) | The solution should correctly identify all words as consistent without error. |
words array contains a string with unicode characters not in allowed | The solution should support unicode characters and correctly identify such a word as inconsistent (using a set would handle this well). |
Large input size for the words array (e.g. 10^4 elements) | The chosen data structure (like set for allowed) and algorithm (iterating words) should handle this scale with reasonable time complexity. |