Given a sequence of words , print all anagrams together using STL | Python3 program for finding all anagram pairs in the given array ; Utility function for printing anagram list ; Utility function for storing the vector of strings into HashMap ; Check for sorted string if it already exists ; Push new string to already existing key ; Print utility function for printing all the anagrams ; Driver code ; Initialize vector of strings ; Utility function for storing strings into hashmap
from collections import defaultdict NEW_LINE def printAnagram ( store : dict ) -> None : NEW_LINE INDENT for ( k , v ) in store . items ( ) : NEW_LINE INDENT temp_vec = v NEW_LINE size = len ( temp_vec ) NEW_LINE if ( size > 1 ) : NEW_LINE INDENT for i in range ( size ) : NEW_LINE INDENT print ( temp_vec [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT DEDENT def storeInMap ( vec : list ) -> None : NEW_LINE INDENT store = defaultdict ( lambda : list ) NEW_LINE for i in range ( len ( vec ) ) : NEW_LINE INDENT tempString = vec [ i ] NEW_LINE tempString = ' ' . join ( sorted ( tempString ) ) NEW_LINE if ( tempString not in store ) : NEW_LINE INDENT temp_vec = [ ] NEW_LINE temp_vec . append ( vec [ i ] ) NEW_LINE store [ tempString ] = temp_vec NEW_LINE DEDENT else : NEW_LINE INDENT temp_vec = store [ tempString ] NEW_LINE temp_vec . append ( vec [ i ] ) NEW_LINE store [ tempString ] = temp_vec NEW_LINE DEDENT DEDENT printAnagram ( store ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ ] NEW_LINE arr . append ( " geeksquiz " ) NEW_LINE arr . append ( " geeksforgeeks " ) NEW_LINE arr . append ( " abcd " ) NEW_LINE arr . append ( " forgeeksgeeks " ) NEW_LINE arr . append ( " zuiqkeegs " ) NEW_LINE arr . append ( " cat " ) NEW_LINE arr . append ( " act " ) NEW_LINE arr . append ( " tca " ) NEW_LINE storeInMap ( arr ) NEW_LINE DEDENT