Python solution with set and count

  • 0
        # We build the set of the different char in each string
        setRansom, setMagazine = set(ransomNote), set(magazine)
        # If all the char from ransomNote are not in magazine => False
        if not setRansom.issubset(setMagazine): 
           return False
        # Each different char from range has to appear at least as many 
        # time in magazine
        for element in setRansom:
            if ransomNote.count(element) > magazine.count(element):
                return False
        return True

Log in to reply

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