前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 1684. 统计一致字符串的数目

LeetCode 1684. 统计一致字符串的数目

原创
作者头像
freesan44
修改2021-09-03 11:01:22
2870
修改2021-09-03 11:01:22
举报
文章被收录于专栏:freesan44freesan44

题目

给你一个由不同字符组成的字符串 allowed 和一个字符串数组 words 。如果一个字符串的每一个字符都在 allowed 中,就称这个字符串是 一致字符串 。

请你返回 words 数组中 一致字符串 的数目。

代码语言:txt
复制
示例 1:

输入:allowed = "ab", words = ["ad","bd","aaab","baa","badab"]
输出:2
解释:字符串 "aaab" 和 "baa" 都是一致字符串,因为它们只包含字符 'a' 和 'b' 。
示例 2:

输入:allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"]
输出:7
解释:所有字符串都是一致的。
示例 3:

输入:allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"]
输出:4
解释:字符串 "cc","acd","ac" 和 "d" 是一致字符串。

提示:

1 <= words.length <= 104

1 <= allowed.length <= 26

1 <= wordsi.length <= 10

allowed 中的字符 互不相同 。

wordsi 和 allowed 只包含小写英文字母。

解题思路

代码语言:txt
复制
class Solution:
    def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
        res = 0
        ## 通过Set方式来进行判断抽取,复杂度降低
        allowedSet = set(allowed)
        for word in words:
            wordSet = set(word)
            if wordSet.issubset(allowedSet):
                res += 1
            # res += 1
            # for i in wordSet:
            #     if i not in allowedSet:
            #         res -= 1
            #         break
        return res

if __name__ == '__main__':
    # allowed = "ab"
    # words = ["ad","bd","aaab","baa","badab"]
    allowed = "abc"
    words = ["a", "b", "c", "ab", "ac", "bc", "abc"]
    ret = Solution().countConsistentStrings(allowed, words)
    print(ret)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 解题思路
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档