zoukankan      html  css  js  c++  java
  • 【leetcode 简单】 第八十九题 赎金信

    给定一个赎金信 (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
  • 相关阅读:
    POJ-2386 Lake Counting
    白书-部分和问题
    STL-map/multimap 简述
    STL-set&&multiset 集合
    STL-优先级队列-priority_queue
    挣脱虚无,化身虚无
    C
    B
    A
    STL-list 链表
  • 原文地址:https://www.cnblogs.com/flashBoxer/p/9535938.html
Copyright © 2011-2022 走看看