挑战每日打卡python基础题,坚持100题
come with me !今日练习:判断一个字符串是否是一致字符串
给你一个由不同字符组成的字符串 allowed 和一个字符串数组 words 。如果一个字符串的每一个字符都在 allowed 中,就称这个字符串是 一致字符串 。
请你返回 words 数组中 一致字符串 的数目。
解法一:
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
res = 0
for i in words:
for j in i:
if j not in allowed:
break
else:
res += 1
return res
解法二:
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
res = 0
for i in words:
tmp = set(i)
for j in tmp:
#print(j)
if j not in allowed:
break
else:
res += 1
return res
解法三:
union() 返回两个集合的并集 |
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
res = 0
for i in words:
tmp = set(i)
all2 = set(allowed)
print(all2)
uni2 = tmp.union(all2)
if all2 == uni2:
res += 1
return res
pop() 随机移除元素
remove() 移除指定元素