Words_count_map_dict.update()įor val in words_count_map_dict. If temp_mapper in words_count_map_dict.keys(): Temp_mapper = alphabet_count_mapper(word) import sysĪlpha_count_dict = dict(zip('abcdefghijklmnopqrstuvwxyz',*26)) You can refer to this SO post: Comparing anagrams using prime numbers, and here is a sample python implementation.ġ.create a dictionary of alphabets/characters with initial count as 0.Ģ.keep count of all the alphabets in the word and increment the count in the above alphabet dict.ģ.create alphabet count dict and return the tuple of the values of alphabet dict.ġ.create a dictionary with alphabet count tuple as key and the count of the number of occurences against it.Ģ.iterate over the above dict and if the value > 1, add the value to the anagram count. This is a more advanced solution, that relies on the "multiplicative uniqueness" of prime numbers. Note that constructing Counter is O(l), while sorting each word is O(n*log(l)) where l is the length of the word. "bac", "silenced", "licensed", "declines") Histogram = tuple(Counter(word).items()) # build a hashable histogram Here is the code: from collections import Counter, defaultdict
0 Comments
Leave a Reply. |