给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回 true ;否则返回 false。
(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。)
注意:
你可以假设两个字符串均只含有小写字母。
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
from collections import Counter class Solution( ): 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 numpy数组及多维数组实例讲解
下一篇 :
etcd 在超大规模数据场景下的性能优化
-
aPaaS平台是什么?aPaaS与PaaS有什么区别?
2026-05-18栏目: 教程
-
【从入门到放弃-ZooKeeper】ZooKeeper实战-分布式队列 | 9月18号栖夜读
2026-05-18栏目: 教程
-
Docker日志收集最佳实践
2026-05-18栏目: 教程
-
怎样有效的治理僵尸网络?
2026-05-18栏目: 教程
-
时间和空间的完美统一!阿里云时空数据库正式商业化
2026-05-18栏目: 教程
