几天没做,先做道简单的哈。这题就是将第一个string分解,如果第2个string没有相应的字母,或者字母的数量少于第一个,就说明不匹配。用python的collections包来解决。
from collections import Counter
class Solution(object):
def canConstruct(self, ransomNote, magazine):
"""
:type ransomNote: str
:type magazine: str
:rtype: bool
"""
if ransomNote == '':
return True
if magazine == '':
return False
note = Counter(ransomNote)
mag = Counter(magazine)
for i in note.keys():
print i
if None == mag.get(i) or note[i] > mag.get(i):
return False
else:
return True