Write an algorithm to solve word ladders such as Word Meld 1
or Word Meld 2
. Input consists of the
starting and ending words and the maximum number of steps allowed. Available are files containing lists of
words of any given length; call one such file, say WORDS5.TXT containing a list of words of 5 letters each, and so on.
This problem is the interview question for my company. So far we have solutions in a range of languages although my personal favourites are in Perl and Tcl.
The offered solution by SilverKnight employs a depth-first search - which is the most common 'good' answer - but you can speed the search up by doing a double-headed search, ie, start at both the start -and- end words and check for either tree adding a node that already exists in the other search tree.
If you intend to run many word ladder problems against your program, you could always go for a long preperation time (and thereafter a short solution time) by building a graph of words of length N that differ by one character and storing it to disc. That way, all you've got to do is a shortest-path solution over a graph (Dijkstra's Algorithm).
Posted by Bruce
on 2003-09-26 08:42:04