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

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

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

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/count-the-number-of-consistent-strings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

LeetCode 1684. 统计一致字符串的数目插图
OpenCV步步精深-可心科创工作室

思路:直接找。

python3实现:

class Solution:
    def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
        list_allowed = list(allowed)
        count = 0

        for elem in words:
            dic_elem = collections.Counter(elem)
            tmp = 0
            std = len(dic_elem)

            for k in dic_elem:
                if k not in list_allowed:
                    break
                else:
                    tmp += 1
            if tmp == std:
                count += 1
            
        return count
LeetCode 1684. 统计一致字符串的数目插图1
OpenCV步步精深-可心科创工作室

更多干货见 OpenCV步步精深

作者 admin

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注