Skip to content
Veltoryx Technologies
ProProblem 118● HardLeetCode #127 ↗4 min readUpdated 30 October 2026

Word Ladder

Find the length of the shortest transformation sequence from beginWord to endWord using BFS.

01The problem

A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s_1 -> s_2 -> ... -> s_k such that:

  • Every adjacent pair of words differs by a single letter.
  • Every s_i for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.
  • s_k == endWord

Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.

InputOutputWhy
beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]5Shortest ladder: hit -> hot -> dot -> dog -> cog (5 words).

Constraints

  • 1 <= beginWord.length <= 10
  • endWord.length == beginWord.length
  • 1 <= wordList.length <= 5000
  • beginWord != endWord
  • All strings in wordList consist of lowercase English letters.
  • All strings in wordList are unique.

Loading the rest of the lesson…