Word Ladder
Given beginWord, endWord, and a word list, return the number of words in the shortest transformation sequence from beginWord to endWord, changing one letter at a time so that every intermediate word is in the list. Return 0 if no such sequence exists.
Open official problem prompt ↗Compute the length of the shortest single-letter-change path from the start word to the end word.
Like navigating a maze of words where each door lets you swap exactly one letter; you want the fewest rooms to walk through to reach the target word.
- Input
- beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
- Output
- 5
- Why
- hit -> hot -> dot -> dog -> cog is 5 words, and no shorter valid chain exists.
1 <= beginWord.length <= 10endWord.length == beginWord.length1 <= wordList.length <= 5000wordList[i].length == beginWord.lengthAll words consist of lowercase English lettersbeginWord != endWordAll words in wordList are unique