127. Word Ladder
Practice Word Ladder for free on NudgeCode: Hard, Graph pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
A ladder from beginWord to endWord is a sequence of words where each step changes exactly one letter, and every word after beginWord must be in wordList. beginWord itself does not need to be in the list. Return the number of words in the shortest ladder (including both ends), or 0 if no ladder exists.
Example
beginWord = "hit", endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"] -> 5
(hit -> hot -> dot -> dog -> cog)
Constraints
- 1 <= beginWord.length <= 10
- endWord and every word in wordList have the same length as beginWord
- 1 <= wordList.length <= 5000
- All words are lowercase;
beginWord != endWord; wordList has no duplicates