Search⌘ K
AI Features

Word Ladder

Explore the Word Ladder problem to understand how to find the shortest transformation sequence from a source to a destination word using breadth-first search techniques on tree structures. Learn to efficiently handle constraints and implement a solution that reflects common coding interview patterns, enhancing your problem-solving skills.

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 main.go in the following coding playground. We have provided some useful code templates in the other file that you may build on to solve this problem.

Go
usercode > main.go
package main
func wordLadder(src string, dest string, words []string) int {
// Replace this placeholder return statement with your code
return -1
}
Word Ladder