Word Ladder (Shortest Transformation)
Problem
Given beginWord, endWord, and a word list, find the length of the shortest transformation sequence from beginWord to endWord, changing exactly one letter at a time, where every intermediate word must be in the list. Return 0 if no such sequence exists.
Input / Output
- Input:
beginWord,endWord, andwordList(all the same length). - Output: the number of words in the shortest transformation sequence (including both ends), or 0.
Constraints
- 1 ≤ wordList ≤ 5000; all words are the same length.
- Only words present in the list count as valid intermediate steps.
Example
hit → cogvia[hot,dot,dog,lot,log,cog]→ 5 (hit → hot → dot → dog → cog).
added …