Asked at
Word Ladder
HardVerifiedBFSGraphString Matching~35 min
Transform beginWord into endWord by changing one letter at a time. Every intermediate word must be in wordList, and each step changes exactly one character.
Return the number of words in the shortest such sequence (counting both ends), or 0 if none exists. The input arrives as a single object { beginWord, endWord, wordList }.
Examples
in{ beginWord: "hit", endWord: "cog", wordList: ["hot","dot","dog","lot","log","cog"] }
out5
hit→hot→dot→dog→cog has 5 words.
in{ beginWord: "hit", endWord: "cog", wordList: ["hot","dot","dog","lot","log"] }
out0
endWord is not in the list, so no sequence exists.
Constraints
- All words have the same length.
- 1 ≤ word.length ≤ 10
- Words contain lowercase letters only.
- beginWord may or may not be in wordList.
Get help
🔑
Sign in to solve
Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.