Recently, I attended an interview and faced a good question regarding hash collisions.
Question : Given a list of strings, print out the anagrams together.
Example :
i/p : {act, god, animal, dog, cat}
o/p : act, cat, dog, god
I want to create hashmap and put the word as key and value as list of anagrams
To avoid collision, I want to generate unique hash code for anagrams instead of sorting and using the sorted word as key.
I am looking for hash algorithm which take care of collision other than using chaining. I want algorithm to generate same hash code for both act and cat... so that it will add next word to the value list
Can anyone suggest a good algorithm ?