Python solution with explanation


  • 0
    G

    Solution

    Ransom Note https://leetcode.com/problems/ransom-note/?tab=Description

    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.