You are viewing a single comment's thread. Return to all comments →
Solution in O(M+N) using a dictionary. Detailed explanation here: Anagrams explanation
def number_needed(str1, str2): dict_chars = dict() for char in str1: if char in dict_chars: dict_chars[char] += 1 else: dict_chars[char] = 1 for char in str2: if char in dict_chars: dict_chars[char] -= 1 else: dict_chars[char] = -1 sum_diff = 0 for char in dict_chars.keys(): sum_diff += abs(dict_chars[char]) return sum_diff a = input().strip() b = input().strip() print(number_needed(a, b))
Seems like cookies are disabled on this browser, please enable them to open this website
Strings: Making Anagrams
You are viewing a single comment's thread. Return to all comments →
Solution in O(M+N) using a dictionary. Detailed explanation here: Anagrams explanation