Hash Tables: Ransom Note

  • + 40 comments

    Using python collections:

    from collections import Counter
    
    def ransom_note(magazine, rasom):
        return (Counter(rasom) - Counter(magazine)) == {}
    

    The result of counters' operator substraction is a counter that shows how many more values magazine is missing to match those present in rasom. When an empty dict is returned, every element in rasom is present enough times in magazine.