python每日打卡题014一致字符串

挑战每日打卡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() 移除指定元素

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容