Python dict solution

  • 0
    import collections
    class Solution(object):
        def canConstruct(self, ransomNote, magazine):
            :type ransomNote: str
            :type magazine: str
            :rtype: bool
            mag_dict = collections.Counter(magazine)
            for c in ransomNote:
                mag_dict[c] = mag_dict.get(c,0) - 1
                if mag_dict[c] < 0: 
                    return False
            return True

Log in to reply

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