Strings: Making Anagrams

  • + 0 comments

    Python solution using one dictionary to count all the letters in the first string, remove all the letters in the second string, and them summing up the absolute counts of each letter for the answer.

    def makeAnagram(a, b):
        letters = {}
        for _ in a: letters[_] = letters.get(_, 0) + 1
        for _ in b: letters[_] = letters.get(_, 0) - 1
        return sum(map(abs, letters.values()))