Facebook Pixel

Word Ladder

Prereq: BFS on Graph

Word Ladder is "A puzzle begins with two words, and to solve the puzzle one must find a chain of other words to link the two, in which two adjacent words (that is, words in successive steps) differ by one letter."

For example: cold → cord → card → ward → warm

Given a start word, an end word, and a list of dictionary words, determine the minimum number of steps to go from the start word to the end word using only words from the dictionary.

Input:

start = "cold"
end = "warm"
word_list = ["cold", "gold", "cord", "sold", "card", "ward", "warm", "tard"]

Output:

4

Explanation: We can go from cold to warm by going through cold → cord → card → ward → warm

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro