Skip to content
DeepForgeby svx
All problems
HardGraph Algorithmsgraph-043

Word Ladder Length

Problem

Find the length of the shortest transformation sequence from begin_word to end_word, where each step changes exactly one letter and every intermediate word must be in word_list.

The sequence length counts both endpoints. Return 0 when no sequence exists. If begin_word equals end_word, return 1. All words have the same length and contain lowercase letters.

Hint

BFS level by level; generate neighbors by substituting each letter in every position.

Test cases

case 1
in: ["hit","cog",["hot","dot","dog","lot","log","cog"]]
expected: 5
case 2
in: ["hit","cog",["hot","dot","dog","lot","log"]]
expected: 0
case 3
in: ["a","c",["a","b","c"]]
expected: 2
case 4
in: ["hot","hot",["hot"]]
expected: 1
case 5
in: ["a","b",["b"]]
expected: 2
Starter codePython
def word_ladder_length(begin_word, end_word, word_list):
    # Your code here
    pass

5 test cases · run your code against them in the editor