Word Ladder (BFS)

Problem Given a beginWord, an endWord, and a dictionary word list, return the length of the shortest transformation sequence from beginWord to endWord, changing exactly one letter at a time, where every intermediate word must be in the word list. Return 0 if no such sequence exists.

Input / Output

  • Input: strings beginWord, endWord; list of words wordList.
  • Output: number of words in the shortest transformation sequence (including both ends), or 0.

Constraints

  • 1 ≤ wordList length ≤ 5000; all words are the same length, lowercase.

Example

  • beginWord = "hit", endWord = "cog", wordList = [hot,dot,dog,lot,log,cog] → 5 (hit→hot→dot→dog→cog).
added …
LeaderboardSalaryAccount