Hard · Graphs
Word Ladder
Given beginWord, endWord and a list of words of the same length, return the number of words in the shortest sequence from beginWord to endWord in which consecutive words differ in exactly one letter and every word after beginWord is in the list, or 0 if no such sequence exists.
Examples
Example 1
{
"beginWord": "hit",
"endWord": "cog",
"wordList": ["hot", "dot", "dog", "lot", "log", "cog"]
}Output: ladder 5
Example 2
{
"beginWord": "hit",
"endWord": "cog",
"wordList": ["hot", "dot", "dog", "lot", "log"]
}Output: ladder 0
Rebuild it in the studio
Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.