本文主要是介绍383. Ransom Note【E】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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:
You may assume that both strings contain only lowercase letters.
canConstruct("a", "b") -> false canConstruct("aa", "ab") -> false canConstruct("aa", "aab") -> true
class Solution(object):def canConstruct(self, ransomNote, magazine):#print ransomNoter = ransomNotem = magazinedic_r = {}dic_m = {}for i in r:dic_r[i] = dic_r.get(i,0) + 1for i in m:dic_m[i] = dic_m.get(i,0) + 1#print dic_r#print dic_mfor k,v in dic_r.items():if k not in dic_m.keys():return Falseif v > dic_m[k]:return Falsereturn True
这篇关于383. Ransom Note【E】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!