Skip to content
AI360Xpert
Beta
Difficulty: HardGraphs

Word Ladder

Problem in Plain English

Change one letter at a time to turn the begin word into the end word, using only words from the given list. Return the word count of the shortest such chain, or 0 when no chain exists.

Problem Statement

Given beginWord, endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord. Each neighbouring pair in the sequence must differ by exactly one letter, and every intermediate word must appear in wordList.

If no valid sequence exists, return 0. The length counts words, so a direct one-letter change has length 2.

Constraints

  • 1 <= beginWord.length <= 10
  • endWord.length == beginWord.length
  • 1 <= wordList.length <= 5000
  • wordList[i].length == beginWord.length
  • beginWord, endWord, and wordList[i] hold lowercase English letters.
  • beginWord != endWord
  • All words in wordList are unique.

Examples

Example 1

Input
beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output
5

The shortest chain is hit to hot to dot to dog to cog, which holds 5 words.

Example 2

Input
beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output
0

The end word cog is missing from the list, so no valid chain exists.

Example 3

Input
beginWord = "hit", endWord = "cog", wordList = ["hot","dot","cog"]
Output
0

The target is present, but hit reaches only hot and dot. Dot and cog differ in two letters, so dictionary membership alone does not guarantee a chain.

Example 4

Input
beginWord = "a", endWord = "c", wordList = ["a","b","c"]
Output
2

A direct change from a to c uses two words. The begin word may appear in the dictionary; marking it visited initially prevents revisiting it.

Intuition

Think of each allowed word as a vertex, with an edge between words differing in exactly one position. Every edge costs one change, so breadth-first search (BFS) explores shorter chains before longer ones. In Example 1, the layers are hit, hot, then dot and lot, then dog and log, then cog: five words. A wildcard such as *ot groups hot, dot and lot without comparing every pair. Once one BFS layer scans that bucket, all its words have already received their earliest possible discovery, so later visits can discard the bucket.

Approaches

Compare All Word Pairs

Solution Details

Reveal Compare All Word Pairs: intuition, complexity, and code

Hints

Hint 1
The shortest chain in steps is a shortest path in an unweighted graph, so think level by level from the begin word.
Hint 2
Two words link when they differ in exactly one position, which defines the graph edges.
Hint 3
Wildcard buckets such as h*t group one-apart words together, so neighbours come from a map read instead of a full list scan.

Edge Cases

  • The end word is missing from the list, as in Example 2, so the answer is 0 before any search starts.
  • The begin word sits one letter from the end word, and the answer is 2 after a single expansion.
  • The end word is present but cut off from the begin word, so the queue drains and the answer is 0.

Common Mistakes and Interview Tips

  • Skipping the visited set, which lets the walk bounce between neighbours like hot and dot forever.
  • Keeping wildcard buckets after scanning them repeats member visits; remove each bucket to justify the faster bound.
  • Returning the level count instead of the word count, which undercounts the chain by one.

Key Takeaway

When edges are defined by a small local change, index items by their change patterns so neighbour lookup skips the pairwise scan.