[LeetCode]#1684. Count the Number of Consistent Strings
Dec 18, 2020
Environment: Python 3.7
Key technique: for loop, if, break
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'.
Analysis:
- if find not allowed letter, constant n=n+1
- all word number -n =answer
Solution:
class Solution:
def countConsistentStrings(self, allowed, words):
not_match=0
for word in words:
for w in word:
if w not in allowed:
not_match+=1
break
ans=len(words)-not_match
return ans
Submissions:
Reference: