https://leetcode.cn/problems/ransom-note
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
示例 1:
输入:ransomNote = “a”, magazine = “b”
输出:false
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
# 哈希表
from collections import Counter
dicran = Counter(ransomNote)
dicmag = Counter(magazine)
for k, v in dicran.items():
if k in dicmag and v <= dicmag[k]:
continue
else:
return False
return True