Isn't it O(n*m*logm), Shashank? That's the complexity of sorting all n
m-sized strings.
And, Arun, here's a working solution in Python for the algorithm Shashank
suggested:
anagrams.py:
> with open('anagrams.in','r') as f:
> words = f.read().split()
> dictionary = {}
> for word in words:
> letters = list(word)
> letters.sort()
> sorted_word = ''.join(letters)
> if dictionary.has_key(sorted_word):
> dictionary[sorted_word].append(word)
> else:
> dictionary[sorted_word] = [word]
> for i in dictionary:
> if len(dictionary[i]) > 1:
> print ', '.join(dictionary[i]), 'are anagrams.'
anagrams.in:
> add dad abc ced cba
Best regards,
Marcelo Menegali
On Tue, Oct 18, 2011 at 5:55 PM, WgpShashank <[email protected]>wrote:
> Sort the all the string , Calculate hash-value , if the two has same hash
> vale they have to be anagram. put in group on the basis of anagram.
> leme know if i missed anything ?
>
> TC O(nlogm) n =number of words m is length of max string
>
>
> Shashank
> CSE,BIT Mesra
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/algogeeks/-/FvPZTBiPMHgJ.
>
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.