my dsa gallery
Graph / StringBFS + DFS / Shortest Path Reconstruction

Word Ladder II

Problem link: https://leetcode.com/problems/word-ladder-ii/

Problem Statement

Given two words, beginWord and endWord, and a dictionary wordList, return all the shortest transformation sequences from beginWord to endWord. Each adjacent word must differ by exactly one letter.

Input

Two strings (beginWord, endWord) and a list of strings (wordList).

Output

A list of lists containing all shortest paths.

Example

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

Explanation

There are two paths of length 5 that lead from 'hit' to 'cog'. Any other paths are either longer or do not exist.

Brute Force

Intuition

Use simple backtracking (DFS) to explore every possible path from beginWord to endWord.

Approach

  1. From current word, try changing every character (a-z).
  2. If the new word is in the dictionary, recurse.
  3. Keep track of the current path length and only store paths matching the global minimum found so far.
Time: O(26^L * N)Space: O(N)

Complexity Analysis

Exponential time complexity due to exploring all possible paths, most of which are not the shortest.

Optimal Solution

Intuition

A two-step approach:

  1. BFS to find the shortest distance from beginWord to all reachable words and map the 'parent-child' relationships.
  2. Backtracking (DFS) to traverse the map from endWord back to beginWord (or vice versa) to build only the shortest paths.

Approach

  1. Use a Set for wordList for O(1) lookups.
  2. BFS Phase: Store the level/distance of each word from beginWord in a HashMap<String, Integer>. This ensures we only move 'forward' in levels during reconstruction.
  3. DFS Phase: Start from endWord. For the current word, look at all neighbors (1-char diff). If a neighbor's level is exactly currentLevel - 1, it is part of a shortest path. Recurse until beginWord is reached.
  4. Reverse the paths found if you backtracked from end to start.

Dry Run

begin: 'hit', end: 'cog'. BFS: hit(0) -> hot(1) -> {dot, lot}(2) -> {dog, log}(3) -> cog(4). DFS from 'cog': Finds neighbors at level 3 ('dog', 'log'), then their neighbors at level 2, etc.

Code (Java)

Time: O(N * 26 * L + K * L)Space: O(N * L + K * L)

Complexity Analysis

N is words in list, L is word length, K is number of shortest paths. BFS takes O(N * 26 * L). DFS time depends on the number of paths.

Quick Revision (Brute Force)

  • Simple DFS
  • Inefficient: visits non-shortest paths

Quick Revision (Optimal)

  • BFS + Backtracking
  • BFS stores min steps to each word
  • DFS reconstructs paths using level check: (level[neighbor] == level[current] - 1)
  • O(N * 26 * L) time

Study Photos

Upload screenshots/notes for this specific problem.

Drag & drop or click to select
Drop an image anywhere in this box to upload.
No photos uploaded till now.

Comments

Stored in D1. Login required.
No comments yet.