 All Problems
Word Ladder II
hard
topological sort
bfs
graph
backtracking
amazon
google
facebook
microsoft

Given beginWord, endWord, and a wordList, find all shortest transformation sequences from beginWord to endWord, where each step changes exactly one letter and every word must be in wordList.

Example 1:

Input: beginWord="hit", endWord="cog", wordList=["hot","dot","dog","lot","log","cog"]
Output: [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]

Example 2:

Input: beginWord="hit", endWord="cog", wordList=["hot","dot","dog","lot","log"]
Output: []

Constraints:

  • 1 <= beginWord.length <= 5
  • 1 <= wordList.length <= 500
Run to check your code against the sample cases, or submit to run every case