If you go big (a large dictionary of words) you may want to move out to a hash table where you sort all the words in the dictionary and hash them into buckets with the key being the sorted version. So then dog and god end up in the same bucket. It gets better, because when you are input odg you find the bucket and return the entire bucket, its ALL the anagrams. Also, you only do the dictionary one time, then save it to a file so you can just load it already organized and ready to roll.
-> be sure to convert to all upper or all lower case
at this point you won't even loop. The whole thing collapses into
read the dictionary file
get input
return/print the result bucket
lots of problems are like this, where a file (or for smaller problems, a hard coded vector) with the 'answers' requires a throw-away program to generate but once you have the answers the rest of the program is trivial. Another example is factorials... a vector "fact" with the answers such that out = fact[in] gets rid of a loop to compute the values in favor of just having the answer in hand.
its notably more complicated if you need to do like text in, multiple words out (theater -> the,rate )
Last edited on