Word Ladder

Free with account Updated Nov 19, 2026 · 7 min read
On this page

    Problem statement

    Given a start word, a target word and a dictionary, return the number of words in the shortest sequence that turns the start word into the target. Each step changes exactly one letter, and every word after the start must be in the dictionary. All words have the same length. If no sequence exists, return 0.

    Under the word puzzle this is a shortest-path question on an unweighted graph, which is why it also shows up as "how many degrees of connection separate two members of a social network". Original problem: LeetCode 127.

    Example

    lead → load → goad → gold: three one-letter changes, four words.
    lead → load → goad → gold: three one-letter changes, four words.
    start = "lead", target = "gold"
    dictionary = [load, goad, gold, lend, bold, road, goat]
    Output: 4          # lead -> load -> goad -> gold
    start = "lead", target = "gold", dictionary = [load, goad, bold]
    Output: 0          # "gold" is not in the dictionary

    Constraints (assumed)

    • 1 ≤ word length L ≤ 10; up to 5,000 dictionary words; lowercase letters only.
    • The start word does not have to be in the dictionary.

    What this question tests

    • Recognizing an implicit graph: words are nodes, one-letter changes are edges.
    • Knowing that BFS gives the shortest path when every edge costs the same.
    • Generating neighbours efficiently instead of comparing every pair of words.

    Questions to ask first

    1. Does the count include the start word? (Here: yes, it counts words, not steps.)
    2. Is the start word in the dictionary? It doesn't have to be. Is the target? If not, the answer is 0.
    3. Are all words the same length and lowercase? (Here: yes.)
    4. Do you want the length or the actual sequence? Returning the path, or all shortest paths, is a common follow-up.