Find the word with most anagrams in a given sentence | Function to find the word with maximum number of anagrams ; Primes assigned to 26 alphabets ; Stores the product and word mappings ; Stores the frequencies of products ; Calculate the product of primes assigned ; If product already exists ; Otherwise ; Fetch the most frequent product ; Return a string with that product ; Driver Code
def largestAnagramGrp ( arr ) : NEW_LINE INDENT prime = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 , 101 ] NEW_LINE max = - 1 NEW_LINE maxpdt = - 1 NEW_LINE W = { } NEW_LINE P = { } NEW_LINE for temp in arr : NEW_LINE INDENT c = [ i for i in temp ] NEW_LINE pdt = 1 NEW_LINE for t in c : NEW_LINE INDENT pdt *= prime [ ord ( t ) - ord ( ' a ' ) ] NEW_LINE DEDENT if ( pdt in P ) : NEW_LINE INDENT P [ pdt ] = P . get ( pdt , 0 ) + 1 NEW_LINE DEDENT else : NEW_LINE INDENT W [ pdt ] = temp NEW_LINE P [ pdt ] = 1 NEW_LINE DEDENT DEDENT for e in P : NEW_LINE INDENT if ( max < P [ e ] ) : NEW_LINE INDENT max = P [ e ] NEW_LINE maxpdt = e NEW_LINE DEDENT DEDENT return W [ maxpdt ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " please ▁ be ▁ silent ▁ and ▁ listen ▁ to ▁ what ▁ the ▁ professor ▁ says " NEW_LINE arr = S . split ( " ▁ " ) NEW_LINE print ( largestAnagramGrp ( arr ) ) NEW_LINE DEDENT