Loading...
You are given two words beginWord and endWord, and a dictionary wordList of words the same length as beginWord (lowercase, unique; beginWord itself does not need to be in the list).
A transformation sequence from beginWord to endWord is a sequence beginWord -> s1 -> s2 -> ... -> sk where every adjacent pair differs in exactly one letter, every word s1 through sk is in wordList, and sk == endWord.
Return the number of words in the shortest such sequence, counting both ends. If no such sequence exists, return 0.
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: "hit" -> "hot" -> "dot" -> "dog" -> "cog" has 5 words.
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output: 0
Explanation: endWord is not in the list, so no sequence exists.
beginWord.length ≤10endWord.length = beginWord.lengthwordList.length ≤5000wordList entries are uniquebeginWord = endWordClick "Run" to test with sample cases or "Submit" to run all tests.