Here is the python code for the similar problem:
http://codercharts.com/puzzle/its-raining-anagrams
The first command line parameter is dictionary file, the second is the
file with checked words.
The idea is to preprocess the dictionary in few steps:
1. calc the set of lengths of the checked words.
2. process the dictionary words having the length in the set of the
step 1:
sort the letters of the every processed word - it will be the key
of the index
add add the word to the index.
3. For every checked word calc the key (sort the letters of the word)
and get the list
of anagrams from the index.
That's all.
============== CODE ====================
import sys
checkwords = open(sys.argv[2]).read().split()
lens = set(len(word) for word in checkwords)
index = {}
for word in open(sys.argv[1]).read().split():
if len(word) in lens:
index.setdefault(''.join(sorted(word)), []).append(word)
for word in checkwords:
anagrams = index[''.join(sorted(word))]
print str(len(anagrams)) + '\n' + '\n'.join(anagrams)
--
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.