细妹按靓,贝托蒂嘉,任瑟雍安昭熙
给定一个赎金信 (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
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论