Word Ladder
Part of Practice: BFS Waves
Given a start word, an end word, and a dictionary of words (all the same length, up to 10 letters, dictionary up to 5000 words), find the length of the shortest transformation sequence from start to end, where each step changes exactly one letter and every intermediate word must be in the dictionary. The length counts words, including both endpoints. Return 0 if no sequence exists (in particular, if the end word is not in the dictionary). The start word does not need to be in the dictionary.
Examples
- Input:
- beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
- Output:
- 5
One shortest sequence is hit → hot → dot → dog → cog, which contains 5 words.
- Input:
- beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
- Output:
- 0
"cog" is not in the dictionary, so the transformation can never legally end there.
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.