Problem Overview
The Word Ladder problem (LeetCode 127) asks to find the shortest transformation sequence length from beginWord to endWord using a given wordList, where each step changes one letter and the new word must exist in wordList. It leverages BFS for shortest path, Hash Table (set for O(1) lookups), and String manipulation.[1][3]
Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.
| Example | beginWord | endWord | wordList | Output | Explanation | |---------|-----------|---------|---------------------------|--------|-------------| | 1 | "hit" | "cog" | ["hot","dot","dog","lot","log","cog"] | 5 | One shortest sequence: hit → hot → dot → dog → cog. [1] | | 2 | "hit" | "cog" | ["hot","dot","dog","lot","log"] | 0 | endWord "cog" not in wordList. [1] |
Additional Notes on Examples: