Skip to interview prep
Apexed
Problem list122 / 125

122. Word Ladder Length

HardGraphs

Transform begin into end by changing one letter at a time, where every intermediate word (and end) must be in words. begin itself need not be in the list. Return the number of words in the shortest such sequence, counting both begin and end, or 0 if it is impossible.

All words have the same length and use lowercase letters; begin differs from end.

Example 1

Input: begin = "hit", end = "cog", words = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: hit → hot → dot → dog → cog

Constraints

  • 1 ≤ word length ≤ 10
  • 1 ≤ words.length ≤ 5000

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Word Ladder Length — coding interview practice — Apexed