Find the word with most anagrams in a given sentence | Java 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
import java . util . * ; class GFG { private static String largestAnagramGrp ( 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 ; HashMap < Long , String > W = new HashMap < > ( ) ; HashMap < Long , Integer > P = new HashMap < > ( ) ; for ( String temp : arr ) { char c [ ] = temp . toCharArray ( ) ; long pdt = 1 ; for ( char t : c ) { pdt *= prime [ t - ' a ' ] ; } if ( P . containsKey ( pdt ) ) { P . put ( pdt , P . get ( pdt ) + 1 ) ; } else { W . put ( pdt , temp ) ; P . put ( pdt , 1 ) ; } } for ( Map . Entry < Long , Integer > e : P . entrySet ( ) ) { if ( max < e . getValue ( ) ) { max = e . getValue ( ) ; maxpdt = e . getKey ( ) ; } } return W . get ( maxpdt ) ; } public static void main ( String args [ ] ) { String S = " please ▁ be ▁ silent ▁ and ▁ listen " + " ▁ to ▁ what ▁ the ▁ professor ▁ says ▁ " ; String arr [ ] = S . split ( " [ ▁ ] + " ) ; System . out . println ( largestAnagramGrp ( arr ) ) ; } }