美文网首页
LeetCode-python 383.赎金信

LeetCode-python 383.赎金信

作者: wzNote | 来源:发表于2019-10-12 20:29 被阅读0次

题目链接
难度:简单       类型: 集合


给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回 true ;否则返回 false。

(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。)

注意:
你可以假设两个字符串均只含有小写字母。

示例

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

解题思路


此题的意思是用后面字符串的字符是否可以组成前面一个字符,需要判断后面各个字符的总数是否大于前面字符的

可以用字典统计两个字符串每个字符的个数,利用collections.Counter()就很方便

代码实现

from collections import Counter
class Solution:
    def canConstruct(self, ransomNote, magazine):
        """
        :type ransomNote: str
        :type magazine: str
        :rtype: bool
        """
        return not Counter(ransomNote) - Counter(magazine)

本文链接:https://www.jianshu.com/p/b69330f71986

相关文章

  • LeetCode-python 383.赎金信

    题目链接难度:简单 类型: 集合 给定一个赎金信 (ransom) 字符串和一个杂志(maga...

  • Leetcode-383 赎金信

    383. 赎金信[https://leetcode-cn.com/problems/ransom-note/] 解...

  • 383. 赎金信

    解题思路 统计magzine字符串中每一个字符串的出现次数,再遍历ransom中每一字母。 STL实现 unord...

  • 383. 赎金信

    题目描述:给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ranso...

  • 「算法」383. 赎金信。

    为了不在赎金信中暴露字迹,从杂志上搜索各个需要的字母,组成单词来表达意思。给你一个赎金信 (ransomNote)...

  • 2022-02-28 「383. 赎金信」

    不知道今天的简单题够判多少年:https://leetcode-cn.com/problems/ransom-no...

  • 赎金信

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/ransom...

  • Leetcode PHP题解--D102 383. Ransom

    D102 383. Ransom Note 题目链接 383. Ransom Note 题目分析 给定一个字符串,...

  • leetcode算法-赎金信

    赎金信 https://github.com/wangeastsea/algorithm-leetCode/tre...

  • 383-赎金信

    赎金信 题目 给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ran...

网友评论

      本文标题:LeetCode-python 383.赎金信

      本文链接:https://www.haomeiwen.com/subject/owecmctx.html