1.(字符串)-判断字符串是否是子集字符串

python:

这里的关键首先是通过collections.defaultdict(int) ,创建默认value是int的字典对象,然后对每一个主串字符统计字母次数;然后对子字符串的每一个字母出现一次就对字典中对应的key字母减1,如果减到小于0或者字母不在字典的key中则返回false,否则全部遍历后返回true

import collections


class Solution:
    def compare_strings(self, A, B):
        tp = collections.defaultdict(int)
        for a in A:
            tp[a] += 1
        for b in B:
            if b not in tp:
                return False
            elif tp[b] <= 0:
                return False
            else:
                tp[b] -= 1
        return True


s = Solution()
a = s.compare_strings('aabbshdjee', 'absje')
print(a)
原文地址:https://www.cnblogs.com/onenoteone/p/12441791.html