Python 1684.py
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
result = 0
for w in words:
result += all(c in allowed for c in w)
return result
Difficulty
and has an acceptance rate of about 83% (200k+ submissions).
Typical patterns to solve this problem:
- Array
- Bit Manipulation
- Hash Table
- String
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a class Solution with a method countConsistentStrings. The method receives a string allowed containing characters that are permitted and a list words of candidate strings. Its purpose is to count how many of the candidate strings consist exclusively of characters found in allowed. The algorithm proceeds by iterating over each word, checking every character against the allowed set, and accumulating a total for words that satisfy the consistency condition.
Key Steps
The first step establishes a counter that will hold the final result.
result = 0
Next, the method iterates over the collection of candidate strings. For each word, it evaluates whether all characters belong to the allowed string. The all function receives a generator expression, which yields a Boolean for each character c in the current word w.
for w in words:
result += all(c in allowed for c in w)
If the generator expression returns True for every character, all yields True, which is implicitly converted to the integer 1 when added to result. Conversely, if any character fails the membership test, all returns False, contributing 0 to the total.
Finally, the method returns the accumulated count.
return result
Edge Cases Handled
When the words list is empty, the for loop body never executes, leaving result at its initial value of 0. The method therefore correctly returns 0 without error.
If allowed is an empty string, the membership test c in allowed will always be False. Consequently, all will return False for any non-empty word, and the method will count only empty strings (if present) as consistent.
Words that contain repeated characters are processed character by character; the generator expression checks each occurrence, ensuring that duplicates do not affect the outcome beyond the basic membership test.
Empty strings within words are considered consistent because the generator expression yields no elements, and all on an empty iterable returns True by definition. Thus each empty string contributes 1 to the final count.
Performance Optimizations
The use of a generator expression inside all avoids the creation of an intermediate list that would otherwise store Boolean results for each character. This lazy evaluation reduces memory overhead, especially for long words.
The all function short-circuits as soon as it encounters a character not present in allowed. This early exit prevents unnecessary checks for the remaining characters of a word that is already known to be inconsistent, saving time when many words contain disallowed characters near the beginning.
By incrementally adding the Boolean result of all directly to result, the code eliminates an explicit conditional branch, relying on Python’s implicit conversion of True to 1 and False to 0. This concise accumulation reduces the number of executed statements inside the loop.
ELI5 - Simplified Explanation
Imagine you have a special box of letter blocks that you are allowed to use. These blocks are like your allowed collection. Now, picture a big pile of words made out of other blocks sitting in front of you.
The code starts by keeping a secret count to remember how many words are okay to build. It looks at each word in the pile one by one. For every word, it checks every single letter inside it. If every letter in that word is also found in your special allowed box, then that word is perfect. When a word is perfect, the count goes up by one. After checking every word in the pile, the final number tells you how many of those words were made only from the letters you were allowed to use.
Code Quality
Complexity Analysis
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| author | solutions | ||||||||||
| www | AlgoMonster | C++ | Python | Java | TS | ||||||
| WalkCCC | C++ | Python | Java | ||||||||
| leetcode.ca | C++ | Python | Java | Go | C | Rust | TS | ||||
| Progiez | C++ | Python | Java | ||||||||
| github | black-shadows | C++ | |||||||||
| charles-wangkai | Java | ||||||||||
| DimitrisJim | C | ||||||||||
| everthis | JS | ||||||||||
| fartem | Ruby | ||||||||||
| fsq | C++ | ||||||||||
| halfrost | Go | ||||||||||
| JoshCrozier | JS | ||||||||||
| Krazune | C++ | ||||||||||
| lkwq007 | Python | ||||||||||
| muhammadfarhankt | Go | ||||||||||
| suxrobGM | C# | ||||||||||
| trung-hn | Python | ||||||||||
| varunu28 | Java | ||||||||||
| YuriSpiridonov | Python | ||||||||||
| AlgoMonster | C++, Python, Java, TypeScript | ||||||||||
| WalkCCC | C++, Python, Java | ||||||||||
| leetcode.ca | C, C++, Go, Java, Python, Rust, TypeScript | ||||||||||
| Progiez | C++, Java, Python | ||||||||||
| black-shadows | C++, C++ | ||||||||||
| charles-wangkai | Java | ||||||||||
| DimitrisJim | C | ||||||||||
| everthis | JavaScript | ||||||||||
| fartem | Ruby | ||||||||||
| fsq | C++ | ||||||||||
| halfrost | Go | ||||||||||
| JoshCrozier | JavaScript | ||||||||||
| Krazune | C++ | ||||||||||
| lkwq007 | Python | ||||||||||
| muhammadfarhankt | Go | ||||||||||
| suxrobGM | C# | ||||||||||
| trung-hn | Python | ||||||||||
| varunu28 | Java | ||||||||||
| YuriSpiridonov | Python |
Videos
Solved On The Same Day
- problem 1550: Three Consecutive Odds
- problem 1629: Slowest Key
- problem 1637: Widest Vertical Area Between Two Points Containing No Points
- problem 1630: Arithmetic Subarrays
- problem 1663: Smallest String With A Given Numeric Value
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality