统计一致字符串的数目
给一个由不同字符组成的字符串 allowed 和一个字符串数组 words 。如果一个字符串的每一个字符都在 allowed 中,就称这个字符串是 一致字符串 。返回 words 数组中 一致字符串 的数目。
采用遍历的方法解题。
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
mask = 0
for c in allowed:
mask |= 1 << (ord(c) - ord('a'))
res = 0
for word in words:
mask1 = 0
for c in word:
mask1 |= 1 << (ord(c) - ord('a'))
res += (mask1 | mask) == mask
return res