LeetCode 1684 Count the Number of Consistent Strings in Python | Easy Coding Tutorial for Beginners

Published: 24 May 2025
on channel: JR: Educational Channel
17
0

Solve LeetCode 1684 "Count the Number of Consistent Strings" in Python with this beginner-friendly coding tutorial! This problem asks you to count how many strings in a list consist only of characters from a given `allowed` string (e.g., allowed = "ab", words = ["ad","bd","aaab","baa","badab"], return 2). We’ll use a list comprehension to check each word, and explore a more efficient solution using a set for faster lookups. Perfect for Python learners, coding beginners, or anyone prepping for coding interviews!

🔍 *What You'll Learn:*
Understanding LeetCode 1684’s requirements
Using list comprehension to check string consistency
Optimizing with a set for faster character lookups
Testing with example cases

💻 *Code Used in This Video:*
class Solution(object):
def countConsistentStrings(self, allowed, words):
"""
:type allowed: str
:type words: List[str]
:rtype: int
"""
return len([word for word in words if all(char in allowed for char in word)])

Test cases
solution = Solution()

Test case 1: Mix of consistent and inconsistent strings
print(solution.countConsistentStrings("ab", ["ad", "bd", "aaab", "baa", "badab"])) # Output: 2
"aaab" and "baa" consist only of 'a' and 'b', which are in allowed

Test case 2: All consistent strings
print(solution.countConsistentStrings("abc", ["a", "b", "c"])) # Output: 3
All words consist only of characters in allowed

Test case 3: No consistent strings
print(solution.countConsistentStrings("a", ["b", "c", "d"])) # Output: 0
No words consist only of 'a'

Test case 4: Empty words list
print(solution.countConsistentStrings("abc", [])) # Output: 0
No words to check

Optimized: Using a set for faster lookups
class SolutionOptimized(object):
def countConsistentStrings(self, allowed, words):
"""
:type allowed: str
:type words: List[str]
:rtype: int
"""
allowed_set = set(allowed) # Convert allowed string to a set
return sum(1 for word in words if all(char in allowed_set for char in word))

solution_opt = SolutionOptimized()
print("\nOptimized solution:")
print(solution_opt.countConsistentStrings("ab", ["ad", "bd", "aaab", "baa", "badab"])) # Output: 2
print(solution_opt.countConsistentStrings("abc", ["a", "b", "c"])) # Output: 3

🌟 *Why Solve LeetCode 1684?*
This problem is a great introduction to string manipulation and set operations in Python, a common topic in coding interviews! The list comprehension solution has a time complexity of O(n * m * k) where n is the number of words, m is the average word length, and k is the length of allowed (due to string lookups). The optimized solution using a set reduces it to O(n * m) with O(k) space for the set. We’ll explore both methods to count consistent strings efficiently. Master this, and you’ll be ready for more advanced LeetCode challenges!

📚 *Who’s This For?*
Python beginners learning coding
Coding enthusiasts tackling LeetCode problems
Developers prepping for technical interviews

👍 Like, subscribe, and comment: What LeetCode problem should we solve next? Next up: More LeetCode string problems—stay tuned!

#LeetCodeTutorial #ConsistentStrings #PythonCoding #LearnCoding #InterviewPrep


On this page of the site you can watch the video online LeetCode 1684 Count the Number of Consistent Strings in Python | Easy Coding Tutorial for Beginners with a duration of hours minute second in good quality, which was uploaded by the user JR: Educational Channel 24 May 2025, share the link with friends and acquaintances, this video has already been watched 17 times on youtube and it was liked by 0 viewers. Enjoy your viewing!