Back to Practice
#0243
Word Ladder Length
HardDSA40 min25 XP
Problem
Each move changes exactly one letter and must create a word in word_list. Return the number of words in the shortest sequence from begin to end, or 0 if impossible.
Why This Matters
Word Ladder is a strong reminder that shortest unweighted path means BFS, even when the graph is hidden inside strings.
Function Signature
def ladder_length(begin, end, word_list):
Examples
Example 1
Inputbegin = "hit", end = "cog", word_list = ["hot", "dot", "dog", "lot", "log", "cog"]
Output5
One shortest path is hit -> hot -> dot -> dog -> cog.
Constraints
- Return the exact requested value.
- Handle empty or small inputs when the prompt allows them.
- Prefer the target DSA pattern over brute force when the input can grow.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Reachable path
Inputhit to cog
Expected5
BFS finds the shortest sequence length.
Missing end
Inputend not in list
Expected0
The final word must be available as a move.
Hidden Test Categories
begin equals end style casesmany decoy wordsno connecting bridge