1
.问题描述
给定一个表示勒索信内容的字符串和另一个表示杂志内容字符串,
写一个方法判断能否通过剪下杂志中的内容构造出这封勒索信,若可
以,返回
T rue
,否则返回
False
。注:杂志字符串中的每一个字符仅能
在勒索信中使用一次。
2
.问题示例
输入
ransom Note="aa"
,
m agazine="aab"
,输出
T rue
,勒索信的内
容可以从杂志内容剪辑而来。
3
.代码实现
class Solution:
def cansetup(self,ransomnote,magazine):
arr = [0]*26
for x in magazine:
arr[ord(x) - ord('a')] += 1
for x in ransomnote:
arr[ord(x) - ord('a')] -= 1
if arr[ord(x) - ord('a')] < 0:
return False
return True
if __name__ == "__main__":
s = Solution()
ransomnote = input()
magazine = input()
print(s.cansetup(ransomnote,magazine))
4
.运行结果
输入:
hello
ajdjhhehdchlllco
输出:
True