676. Word Ladder — Shortest Transformation

HardStackQueueBFS

Given beginWord, endWord, and a word list, transform beginWord into endWord by changing one letter at a time so that each intermediate word is in the list. Return the number of words in the shortest transformation sequence (including both endpoints), or 0 if none exists. The input is JSON {beginWord, endWord, wordList}.

Input: JSON {beginWord, endWord, wordList}.

Output: Integer — the shortest 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 reachable.
Example 3
Input: {"beginWord":"a","endWord":"c","wordList":["a","b","c"]}
Output: 2
Explanation: a -> c directly.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →