We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Making Anagrams
You are viewing a single comment's thread. Return to all 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)