您好,欢迎来到尔游网。
搜索
您的当前位置:首页LeetCode赎金信

LeetCode赎金信

来源:尔游网

题目要求:

给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。

如果可以,返回 true ;否则返回 false 。

magazine 中的每个字符只能在 ransomNote 中使用一次。

示例 1:

输入:ransomNote = "a", magazine = "b"
输出:false

解题思路:

如果magazine的长度小于ransomNote的长度,不能构成,返回false,否则统计两个字符串中字母出现的次数,如果magazine的字母次数都大于等于ransomNote,返回true

代码展示:

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        if len(magazine)<len(ransomNote):
            return False
        return not collections.Counter(ransomNote)-collections.Counter(magazine)

运行结果:

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- axer.cn 版权所有 湘ICP备2023022495号-12

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务