269. Word Ladder (Shortest Transformation Length)

HardStringString

Given begin word, end word, and a word list, return the number of words in the shortest transformation sequence from begin to end, changing one letter at a time, where each intermediate word must be in the list. Return 0 if no sequence exists. Input: JSON {begin, end, words}.

Input: JSON {begin, end, words}.

Output: Integer — sequence length or 0.

Examples

Example 1
Input: {"begin":"hit","end":"cog","words":["hot","dot","dog","lot","log","cog"]}
Output: 5
Explanation: hit->hot->dot->dog->cog.
Example 2
Input: {"begin":"hit","end":"cog","words":["hot","dot","dog","lot","log"]}
Output: 0
Explanation: 'cog' not in list.

Constraints

Asked by

AmazonAppleMetaFlipkartBloombergOracle
Solve this problem in the editor →