Find the word with most anagrams in a given sentence | C ++ Program to find the word with most anagrams in a 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestAnagramGrp ( vector < string > arr ) { int 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 } ; int max = -1 ; long maxpdt = -1 ; unordered_map < long long , string > W ; unordered_map < long long , long > P ; for ( string temp : arr ) { long pdt = 1 ; for ( char t : temp ) { pdt *= prime [ t - ' a ' ] ; } if ( P . find ( pdt ) != P . end ( ) ) { P [ pdt ] ++ ; } else { W [ pdt ] = temp ; P [ pdt ] = 1 ; } } for ( auto e : P ) { if ( max < e . second ) { max = e . second ; maxpdt = e . first ; } } return W [ maxpdt ] ; } int main ( ) { char S [ ] = " please ▁ be ▁ silent ▁ and ▁ listen ▁ to ▁ what ▁ the ▁ professor ▁ says ▁ " ; vector < string > arr ; char * token = strtok ( S , " ▁ " ) ; while ( token != NULL ) { arr . push_back ( token ) ; token = strtok ( NULL , " ▁ " ) ; } cout << largestAnagramGrp ( arr ) << endl ; return 0 ; }