Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.
A transformation sequence is valid when:
endWord) must exist in wordList.beginWord does not need to be in wordList.beginWord = "hit" endWord = "cog" wordList = ["hot","dot","dog","lot","log","cog"]
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] Output: 5 Explanation: "hit" -> "hot" -> "dot" -> "dog" -> "cog" is the shortest path, counting 5 words.
beginWord = "hit" endWord = "cog" wordList = ["hot","dot","dog","lot","log"]
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"] Output: 0 Explanation: "cog" is not in wordList, so no valid sequence exists.
1 <= beginWord.length <= 10endWord.length == beginWord.length1 <= wordList.length <= 5000wordList[i].length == beginWord.lengthbeginWord, endWord, and wordList[i] consist of lowercase English letters.beginWord != endWordbeginWord = "hit", endWord = "cog", wordList = ["hot", "dot", "dog", "lot", "log", "cog"]