解题思路
此题我们可以构建两个哈希字典,存储对出现的字符进行计数,满足以下条件
ransom
中字符的在magazine
中均出现ransom
中字符的计数值应小于magzine
的数值
代码
class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: s_1 = {i:0 for i in ransomNote} t_1 = {i:0 for i in magazine} for i in ransomNote: s_1["%s"%i] += 1 for i in magazine: t_1["%s"%i] += 1 for i in s_1.keys(): if i in magazine and s_1[i] <= t_1[i]: continue else: return False return True