LeetCode 3 May Ransom Note 赎金条、杂志、查找

Ransom Note

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines; other wise, it will return false.

Each letter in the magazine string can only be used once in your ransom note.

**Note: **

You may assume that both strings contain only lowercase letters.

**Examples: **

canConstruct("a", "b") -> False

canConstruct("aa", "ab") -> False

canConstruct("aa", "aab") -> True

分析:

一开始对题意的理解错误,认为只是字符串子串匹配的问题。结果发现不是。和现实情形一样,杂志(magazine)是可以被裁切的,可以对其包含的元素任意排序。最简单的想法就是分别统计magazine和ransom中的各个字符出现次数。如果ransom中某一字符出现次数要比magazine中多,那么magazine一定不可以完整表达ransom中的信息,返回False。如果ransom中某一字符在magazine中就没有出现,那么同样不能表达,返回False。

代码一:

分别创建两个字典用来记录各自字符出现次数。最后遍历ransom字典中的关键字,若其不再magazine字典中,那返回False,若其数量大于magazine字典中的数量,返回False。其他的情况则返回True。该方法创建两个字典并对字典中key值进行遍历。

代码二:

只统计magazine中的字符出现次数,遍历ransom中的字符,同时在magazine字典中进行判断。①若字符在magazine字典中未出现,那么返回False。②否则对字典中该字符统计数据进行减1操作,若减1操作后数量大于0则可以继续下一个字符的判断。若小于0则表示magazine中没有足够的字符去进行拼接该ransom。返回False。③当ransom中字符循环一遍之后,没有返回False,则返回True。该方法只创建了一个字典,对另一个字符串进行遍历操作,相比于上一个方法可以减少一次字典的创建。

# Runtime: 84ms
# Memory Usage: 13.9MB
class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        ransomDic = {}
        for slices in ransomNote:
            if not slices in ransomDic:
                ransomDic[slices] = 1
            else:
                ransomDic[slices] += 1
        magazineDic = {}
        for slices in magazine:
            if not slices in magazineDic:
                magazineDic[slices] = 1
            else:
                magazineDic[slices] += 1
        for key in ransomDic:
            if key not in magazineDic:
                return False
            elif ransomDic[key] <= magazineDic[key]:
                    continue
            else:
                return False
        return True
      
# Runtime: 64ms
# Memory Useage: 14.2
class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        magazineDic = {}
        for slices in magazine:
            if not slices in magazineDic:
                magazineDic[slices] = 1
            else:
                magazineDic[slices] += 1
                
        for c in ransomNote:
            if c not in magazineDic:
                return False
            else:
                magazineDic[c] -= 1
                if magazineDic[c] < 0:
                    return False
        return True

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值