Ransom Note

    xiaoxiao2026-03-02  9

    leetcode第383题,这道题比较简单,其实就是查字典问题,字典中必须有足够多的字母来满足写信的要求,同时还要具备应该有的字母。

    直接上代码吧。

    class Solution(object): def canConstruct(self, ransomNote, magazine): """ :type ransomNote: str :type magazine: str :rtype: bool """ n = len(ransomNote) d = {} n2 = len(magazine) for i in range(n2): if magazine[i] not in d: d[magazine[i]] = 1 else: d[magazine[i]] += 1 for i in range(n): if ransomNote[i] not in d: return False else: d[ransomNote[i]] -= 1 if d[ransomNote[i]] < 0: return False print d return True

    转载请注明原文地址: https://ju.6miu.com/read-1307541.html
    最新回复(0)