题目
September 5, 2017 · View on GitHub
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
假设只包含小写字母
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
思路
判断ransomNote中的字符在magezine出现的次数,如果ransomNote中字符出现的次数多余在magezine中出现的此处,那么返回False。
Python
class Solution(object):
def canConstruct(self, ransomNote, magazine):
"""
:type ransomNote: str
:type magazine: str
:rtype: bool
"""
if len(ransomNote) == 0:
return True
for each in list(set(ransomNote)):
if ransomNote.count(each) > magazine.count(each):
return False
return True