DIY: Word Ladder II

Solve the interview question "Word Ladder II" yourself in this lesson.

We'll cover the following

Problem statement

In this scenario, you are given a list of words, a starting_word, and an ending_word. The problem requires you to find all shortest transformation sequences’ from starting_word to ending_word. There are a few conditions for this problem:

  • All the words should be the same length.

  • There should be no duplicates in the given list.

  • starting_word and ending_word should be non-empty.

  • starting_word and ending_word should not be the same.

  • A transition can only occur if two words differ by one character/alphabet.

  • It should return an empty list if there is no such transformation sequence.

  • All the words should contain only lowercase alphabetic characters.

Input

The following is an example input:

word_list = ["hot","dot","dog","lot","log","cog"]
start = "hit"
target = "cog"

Output

The following is an example output:

[
  ["hit","hot","dot","dog","cog"],
  ["hit","hot","lot","log","cog"]
]

It can be seen that hit can be converted to cog by following the above two sequences.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.