Find the size of largest subset of anagram words | Java Program to find the size of largest subset of anagram ; Utility function to find size of largest subset of anagram ; sort the String ; Increment the count of String ; Compute the maximum size of String ; Driver code
import java . util . * ; class GFG { static int largestAnagramSet ( String arr [ ] , int n ) { int maxSize = 0 ; HashMap < String , Integer > count = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { char temp [ ] = arr [ i ] . toCharArray ( ) ; Arrays . sort ( temp ) ; arr [ i ] = new String ( temp ) ; if ( count . containsKey ( arr [ i ] ) ) { count . put ( arr [ i ] , count . get ( arr [ i ] ) + 1 ) ; } else { count . put ( arr [ i ] , 1 ) ; } maxSize = Math . max ( maxSize , count . get ( arr [ i ] ) ) ; } return maxSize ; } public static void main ( String [ ] args ) { String arr [ ] = { " ant " , " magenta " , " magnate " , " tan " , " gnamate " } ; int n = arr . length ; System . out . println ( largestAnagramSet ( arr , n ) ) ; String arr1 [ ] = { " cars " , " bikes " , " arcs " , " steer " } ; n = arr1 . length ; System . out . println ( largestAnagramSet ( arr1 , n ) ) ; } }