当前位置: 移动技术网 > IT编程>脚本编程>Python > 【leetcode 简单】 第八十九题 赎金信

【leetcode 简单】 第八十九题 赎金信

2018年08月26日  | 移动技术网IT编程  | 我要评论

细妹按靓,贝托蒂嘉,任瑟雍安昭熙

给定一个赎金信 (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

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网