Making Anagrams

  • + 0 comments

    I made an efficient code .Instead of using multiple list comprehensions and sum calls, I streamlined it by reading the lines, processing them, and calculating the differences in one go. This approach minimizes intermediate lists and leverages efficient operations.

    import sys from collections import Counter

    Read lines from stdin

    lines = [sys.stdin.readline().strip() for _ in range(2)]

    Count characters in each line

    counts = [Counter(line) for line in lines]

    Calculate the total difference in counts for each letter

    diff = 0 for letter in "abcdefghijklmnopqrstuvwxyz": diff += abs(counts[0].get(letter, 0) - counts[1].get(letter, 0))

    print(diff)