const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Word Ladder LeetCode 127 Guide

Word Ladder (#127) teaches you that graphs exist even when no graph is given. Each word is a node, one-letter differences are edges, and the shortest transformation is a BFS problem hiding in plain sight.

9 min read|

Word Ladder (#127): BFS on a graph you cannot see

Words are nodes, one-letter changes are edges — shortest path on strings

Word Ladder: The Invisible Graph Problem

Word Ladder (#127) is one of those problems that completely changes how you think about graphs. There is no adjacency list. There is no matrix. You are given a start word, an end word, and a dictionary — and somehow this is a graph problem.

The task sounds deceptively simple: transform beginWord into endWord, one letter at a time, where every intermediate word must exist in the given dictionary. Return the length of the shortest transformation sequence. If no such sequence exists, return 0.

What makes word ladder leetcode such a beloved interview question is the mental leap it requires. You have to realize that words are nodes and single-letter differences are edges — and once you see it, BFS gives you the shortest path instantly.

This problem appears frequently at Google, Amazon, and Meta. It tests whether you can recognize an implicit graph and reach for BFS without being handed explicit edges. Let us walk through the solution step by step.

Understanding the Problem

You are given beginWord, endWord, and a wordList (dictionary). You need to find the shortest transformation sequence from beginWord to endWord such that only one letter is changed at a time and each transformed word must exist in wordList.

For example, given beginWord = "hit", endWord = "cog", and wordList = ["hot","dot","dog","lot","log","cog"], the shortest transformation is hit -> hot -> dot -> dog -> cog, which has length 5. Note that the count includes both the start and end words.

There are a few critical rules. The endWord must be in wordList — if it is not, there is no valid transformation and you return 0. The beginWord does not need to be in wordList. All words have the same length, and all words contain only lowercase English letters.

The key insight is understanding what "shortest" means here. You are not looking for any path from beginWord to endWord — you need the shortest one. Whenever you hear "shortest path" in an unweighted context, your first thought should be BFS.

Why Word Ladder Is a Graph Problem

The word ladder leetcode problem does not look like a graph problem at first glance. There are no nodes, no edges, no adjacency list. But graphs do not require explicit structure — they can be implicit.

Think of every word in the dictionary (plus beginWord) as a node. Two nodes are connected by an edge if and only if the words differ by exactly one letter. "hot" and "dot" differ only at index 0, so they are neighbors. "hot" and "dog" differ at two positions, so they are not connected.

Once you see this implicit graph, the problem transforms into a classic shortest path question: find the shortest path from the beginWord node to the endWord node in an unweighted graph. The answer is BFS.

  • Nodes: every word in wordList (plus beginWord)
  • Edges: two words are connected if they differ by exactly one letter
  • Shortest path in an unweighted graph = BFS level-order traversal
  • Each BFS level represents one letter change — the level count is your answer
ℹ️

Interview Insight

Word Ladder (#127) is one of the most frequently asked Hard/Medium-Hard problems at Google — it tests whether you can recognize an implicit graph and apply BFS without being given explicit edges.

BFS Solution for Word Ladder

The BFS approach starts from beginWord and explores all words that are one letter away, then all words two letters away, and so on. The first time you reach endWord, you have found the shortest transformation.

Place beginWord in a queue and add all dictionary words to a set for O(1) lookup. For each word you dequeue, generate every possible one-letter variation by replacing each character position with every letter from a to z. If a generated word exists in the dictionary set, add it to the queue and remove it from the set to prevent revisiting.

Track the transformation length by incrementing a counter each time you finish processing an entire BFS level. When you dequeue endWord, return the current level count. If the queue empties without finding endWord, return 0.

The time complexity is O(n * L * 26) where n is the number of words in the dictionary and L is the word length. For each word, you try 26 replacements at each of L positions. The space complexity is O(n * L) for the set and the queue.

  1. 1Initialize a set from wordList for O(1) lookup. Check if endWord is in the set — if not, return 0.
  2. 2Create a queue with beginWord. Set level = 1.
  3. 3While the queue is not empty, process all words at the current level.
  4. 4For each word, try replacing each character with a-z. If the new word is in the set, add it to the queue and remove it from the set.
  5. 5If you generate endWord, return level + 1.
  6. 6After processing the entire level, increment level by 1.
  7. 7If the queue empties, return 0 — no valid transformation exists.

Optimization: Pattern-Based Adjacency

The brute-force neighbor generation works, but there is an elegant optimization using wildcard patterns. Instead of comparing every pair of words, you pre-build a map from patterns to words.

The idea is simple: for a word like "hot", generate wildcard patterns by replacing each character with a star — "*ot", "h*t", and "ho*". Any two words that share a pattern differ by exactly one letter. "hot" and "hit" both match "h*t", so they are neighbors.

Before BFS begins, iterate through every word in the dictionary and build a hash map where keys are patterns and values are lists of words matching that pattern. During BFS, instead of trying 26 letters at each position, you look up the current word's patterns in the map and get all neighbors directly.

This pattern-based approach is especially powerful when the dictionary is large. The preprocessing takes O(n * L) time and space, but neighbor lookups during BFS become nearly instant. This is the same implicit graph BFS technique used in Open the Lock (#752) and Gene Mutation (#433).

💡

Pro Tip

Generate neighbors by replacing each character with a-z (26 * word_length options) rather than comparing against every word in the dictionary — this is faster when the dictionary is large.

Edge Cases and Variations

Several edge cases can trip you up on the word ladder leetcode problem if you are not careful. Always handle these before diving into BFS.

The most common edge case is when endWord is not in the dictionary. The problem guarantees all intermediate words must be in wordList, and endWord is the final intermediate step. If it is missing, return 0 immediately — no BFS needed.

Another subtle case is when beginWord equals endWord. Most problem statements clarify this will not happen, but defensive coding checks anyway. Also watch for cases where the dictionary is non-empty but no valid path exists — your BFS will naturally return 0 when the queue empties.

Word Ladder II (#126) is the harder variant that asks you to return all shortest transformation sequences, not just the length. This requires BFS to find the shortest distance, then backtracking to reconstruct all paths. It is significantly more complex and a common follow-up in interviews.

  • endWord not in dictionary: return 0 immediately
  • No valid path exists: BFS queue empties, return 0
  • beginWord already in dictionary: still works, just gets removed from set when visited
  • Word Ladder II (#126): find ALL shortest paths — requires BFS + backtracking

What Word Ladder Teaches You

Word Ladder is more than a single problem — it is a gateway to an entire class of implicit graph BFS problems. Once you internalize the pattern of "objects are nodes, valid transitions are edges," you can solve dozens of similar problems.

Open the Lock (#752) uses the same idea: lock states are nodes, single-digit turns are edges. Gene Mutation (#433) maps DNA strings to nodes with single-character mutations as edges. Even Sliding Puzzle (#773) follows this template with board states as nodes and tile swaps as edges.

The core lesson is this: whenever a problem asks for the shortest sequence of transformations between two states, think BFS on an implicit graph. You do not need an adjacency list handed to you — you generate neighbors on the fly.

Practice recognizing these patterns with YeetCode flashcards. The word ladder BFS pattern is one you will see again and again, and the ability to spot an implicit graph in an interview is what separates good candidates from great ones.

⚠️

Watch Out

Word Ladder II (#126) asks for ALL shortest paths, not just the length — this is significantly harder and requires BFS + backtracking. Don't attempt it until you've mastered the basic version.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now