思路非常巧妙,用到了Python的replace(old,new,maxTimes)函数,但要注意该函数并不会原地变化,故需要一步赋值操作。、
执行用时 :52 ms, 在所有 Python3 提交中击败了96.90%的用户
内存消耗 :13.9 MB, 在所有 Python3 提交中击败了5.17%的用户
Code:
class Solution(object):
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
for letter in ransomNote:
if letter in magazine:magazine=magazine.replace(letter,'',1)
else:return False
return True