import collections
class Solution(object):
def canConstruct(self, ransomNote, magazine):
"""
:type ransomNote: str
:type magazine: str
:rtype: bool
"""
mag_dic = collections.Counter(magazine)
for ch in ransomNote:
if ch in mag_dic:
mag_dic[ch] -= 1
# print(ch, mag_dic[ch])
else:
return False
for ch in mag_dic:
if mag_dic[ch] <= -1: # not == -1
return False
return True
再加个测试用例:
"fihjjjjei"
"hjibagacbhadfaefdjaeaebgi"
可以发现,j 的数量减到了 -2 ,所以啊,注意细节。