LeetCode 127 Word Ladder - Example and Complexity Analysis

Walkthrough and analysis

BFS from "hit".

Level 1: "hit". Find neighbors differing by one letter in dictionary: "hot".

Level 2: "hot". Neighbors: "dot", "lot".

Level 3: "dot", "lot". Neighbors: "dog", "log".

Level 4: "dog", "log". Neighbors: "cog" found!

Level 5: Return 5.

For each word, generate 26 × L variations (L = word length). Check against dictionary set. O(nL226)O(n \cdot L^2 \cdot 26) where nn is dictionary size. Space: O(n)O(n).