题目
给你一个由不同字符组成的字符串 allowed 和一个字符串数组 words 。如果一个字符串的每一个字符都在 allowed 中,就称这个字符串是 一致字符串 。
请你返回 words 数组中 一致字符串 的数目。
思路
暴力法枚举words中所有元素的各个字母,利用find判断该字母是否包含在allowed中,如果包含则count++,最后计算count的值是否和原字符串长度相等,如果相等则结果ret+1。
代码
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
allow_set = set(list(allowed))
res = 0
for word in words:
for ch in word:
if ch not in allow_set:
res -= 1
break
res += 1
return res