Search⌘ K
AI Features

Word Ladder

Explore how to solve the Word Ladder problem by applying Tree Breadth-First Search traversal. Understand how to find the shortest transformation sequence between two words by changing one character at a time, using an efficient approach to navigate through the word list.

Statement

Given two words, src and dest, and a list, words, return the number of words in the shortest transformation sequence from src to dest. If no such sequence can be formed, return 00.

A transformation sequence is a sequence of words ((src \to word1word_1 \to word2word_2 \to word3word_3 ...... \to wordjword_j )) that has the following properties:

  • wordj=word_j = dest
  • Every pair of consecutive words differs by a single character.
  • All the words in the sequence are present in the words. The src does not need to be present in words.

Constraints:

  • 11 \leq src.length 10\leq 10

  • src.length == dest.length == words[i].length

  • src \neq dest

  • 11 \leq words.length 5000\leq 5000

  • No duplicates in the words

  • src, dest, and words[i] consist of lowercase English characters.

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Word Ladder

1.

What’s the correct output for the following inputs?

src = “hit”

dest = “cog”

words = [“log”, “dot”, “lot”, “dog”, “hot”, “cog”]

A.

5

B.

6

C.

0

D.

7


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in the following coding playground.

Python
usercode > main.py
def word_ladder(src, dest, words):
# Replace this placeholder return statement with your code
return -1
Word Ladder