1022. Word Ladder I — Minimum Transformations

EasyGraphsBFSGraph

Given beginWord, endWord, and a wordList of equal-length words, return the number of words in the shortest transformation sequence from beginWord to endWord, changing exactly one letter at a time where each intermediate word must be in wordList (endWord must be in wordList). Return 0 if no sequence exists. The input is JSON {beginWord, endWord, wordList}.

Input: JSON {beginWord, endWord, wordList}.

Output: Integer — the sequence length, or 0.

Examples

Example 1
Input: {"beginWord":"hit","endWord":"cog","wordList":["hot","dot","dog","lot","log","cog"]}
Output: 5
Explanation: hit->hot->dot->dog->cog.
Example 2
Input: {"beginWord":"hit","endWord":"cog","wordList":["hot","dot","dog","lot","log"]}
Output: 0
Explanation: endWord not in list.
Example 3
Input: {"beginWord":"a","endWord":"c","wordList":["a","b","c"]}
Output: 2
Explanation: a->c directly.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →