Python solution with explanation

  • 0


    Ransom Note

    from collections import Counter
    class Solution(object):
        def canConstruct(self, ransomNote, magazine):
            :type ransomNote: str
            :type magazine: str
            :rtype: bool
            ransomCounter, magCounter = Counter(ransomNote), Counter(magazine)
            for k,v in ransomCounter.items():
                if k not in magCounter or magCounter[k] < v:
                    return False
            return True

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.