Word Ladder
Problem statement
Given beginWord, endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord.
Every transformation changes exactly one letter, and every transformed word, including endWord, must be in the dictionary. Return 0 when no sequence exists.
Function
ladderLength(beginWord: String, endWord: String, wordList: List<String>) → intExamples
Example 1
beginWord = "hit"endWord = "cog"wordList = ["hot","dot","dog","lot","log","cog"]return = 5A shortest sequence is hit -> hot -> dot -> dog -> cog.
Example 2
beginWord = "hit"endWord = "cog"wordList = ["hot","dot","dog","lot","log"]return = 0Constraints
1 <= beginWord.length <= 10.endWord.length == beginWord.length.1 <= wordList.length <= 5000.wordList[i].length == beginWord.length.beginWord,endWordandwordList[i]consist of lowercase English letters.beginWord != endWord.- All the words in
wordListare unique.