Popular Posts

Monday, May 2, 2011

Anagrams in a Dictionary

Q)Given a dictionary of words.Returns the set of all words grouped into subsets of words that are all 
anagrams of each other.
Algorithm :
1)Create a hashmap of String and Set (Map<String,set<String>>)
2)Read a word from the dictionary and sort the word.
   i) If the sorted word(key) word is not present in the Map,crate an empty set and add this key to      set.Insert key,list pair into map.
  ii)Else {
        If key is present in the map,get the list and add the key to set.Again put this new key value pair into map.     }

3)Iterate through all keys of map,get the set and print key,values of set.
      
ps:An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; e.g., orchestra = carthorseA decimal point = I'm a dot in place.

No comments:

Post a Comment