Yelp Interview Question

You have an array with words. Print them by anagrams groups

Interview Answers

Anonymous

Apr 9, 2015

1.Use Hashtable with key as sorted words from the given word array and value as ArrayList of all the strings that are anagrams of the same. 2. Iterate through hashtable print the list values.

9

Anonymous

Oct 24, 2015

It is a simple one. For each of the words, generate a descriptor or a hash table with 26 keys as alphabets a, b, c, d, ... z and for corresponding values just have the frequency of occurrence of those characters in the word [0 for no-occurrence]. Now, for n words you will generate n descriptors or hash tables. Now you can compare the first descriptor with rest of the descriptors using hamming distance concept and mark the ones that are similar and counted. In the worst case, this will give you a complexity of O(n^2) [n-2 + n-1 + n-3 + ... + 1 -> Comparisons of descriptors]. There could be other ways to optimize this O(n^2) complexity time as well.