1. 程式人生 > >【leetcode 簡單】 第八十九題 贖金信

【leetcode 簡單】 第八十九題 贖金信

第一個 div nbsp for 第一個字符 span counter map 註意

給定一個贖金信 (ransom) 字符串和一個雜誌(magazine)字符串,判斷第一個字符串ransom能不能由第二個字符串magazines裏面的字符構成。如果可以構成,返回 true ;否則返回 false。

(題目說明:為了不暴露贖金信字跡,要從雜誌上搜索各個需要的字母,組成單詞來表達意思。)

註意:

你可以假設兩個字符串均只含有小寫字母。

canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true


from collections import
Counter class Solution(object): def canConstruct(self, ransomNote, magazine): """ :type ransomNote: str :type magazine: str :rtype: bool """ a,b = map(Counter,(ransomNote,magazine)) for i in a: if i not in b: return
False if i in b and a[i] > b[i]: return False return True

【leetcode 簡單】 第八十九題 贖金信