Google Interview Question

How to find anagrams in a sentence ?

Interview Answers

Anonymous

Sep 30, 2011

Hey Take all alphabet and map with a prime number. Like A = 2, B=3, C=5, D=7, E=11 etc. Now read each word in the sentence and each word char by char and make a product of those with mapping prime num. And store them in a map with key as product. Each anagram will have same product of prime numbers.

4

Anonymous

Apr 22, 2012

Do you mean each word could be an anagram of another in the same sentence? Or you have a list of words? How big is the input?

Anonymous

Sep 30, 2011

1. prep your lexicon, sort each word of your dictionary, the resort the whole dictionary. 2. all duplicate entries are anagrams, make a list of duplicates. 3. sort each word in your sentence, and check if it exists in your new lexicon. A hashmap is a good way to go, but a binary search will work fine in a pinch. 4. consider building a couple indexes to make the process more robust.

Anonymous

Sep 15, 2011

Use Hashmaps with words as key to find anagrams