力扣题目:
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
if len(ransomNote)>len(magazine):
return False
return not collections.Counter(ransomNote) - collections.Counter(magazine)
return not collections.Counter(ransomNote) - collections.Counter(magazine)
这句代码中,最后返回值是Ture或False
用collections.Counter(dict)实现
一些相关知识点下文会介绍。
【字典】类型也可以相加减,用collections.Counter(dict)实现,但结果会自动舍掉value值<=0的dict.items()
示例如下:
字典的相加减操作需要用到python内置函数class:collections.Counter([iterable-or-mapping])
#加入有两个字典dict如下:
x &#