A transformation sequence changes exactly one letter at a time, and every intermediate word must exist in wordList. Return the number of words in the shortest valid sequence from beginWord to endWord, including both endpoints. If no sequence exists, return 0.
All words have the same length.
Input / output
beginWord: string, endWord: string, wordList: string[]0 if impossibleExamples
beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] returns 5 for hit -> hot -> dot -> dog -> cog.beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"] returns 0.beginWord = "lost", endWord = "cost", wordList = ["most","fist","lost","cost","fish"] returns 2.Constraints
1 <= beginWord.length <= 10beginWord.length == endWord.length1 <= wordList.length <= 5000wordList[i].length == beginWord.lengthTarget complexity
endWord, that path is guaranteed shortest.Hints
Follow-up What would a bidirectional BFS buy you here, and when is it worth the extra implementation complexity?