Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Climbing the Word Ladder (Main Thread)

Here is the interview question prompt, presented for reference.

Imagine two words placed at the two ends of a ladder. Can you reach one word from another using intermediate words as rings of the ladder? Each successive or transition word has a difference of only one letter from its predecessor, and must belong to a provided dictionary. Can we achieve this using the shortest possible number of rings, and return that number?

The Problem

Suppose we are given:

  • a dictionary of words
  • a start word
  • an end word

We have to find a minimum length sequence of words so that the start word is transformed into the end word. A word is only transformable to the other if they have a difference of one letter. Consider a dictionary, start word startW, and end word endW below:

Dictionary: {hope, hole, bold, hold, cold, sold, dold}
startW: bold
endW: hope

Two possible sequences of transformations are given below:

Sequence 1: bold->cold->sold->hold->hole->hope
Sequence 2: bold->hold->hole->hope

The length of the second sequence is 4, which is shorter than sequence 1 with a length of 6. Hence the acceptable answer is 4. We make the following assumptions for this problem:

  1. All words in the dictionary are of the same length.
  2. The end word is present in the dictionary. If it is not present, 0 is returned.
  3. The comparison with words in the dictionary is case sensitive. So abc is not the same as ABC.

Constraints

  • The number of words in the dictionary <= 50
  • The length of each word in the dictionary and start word <= 50
  • The strings will only consist of lowercase and uppercase letters
  • Let n be the length of each word and m be the number of words
  • Expected time complexity :O((m^2)*n)
  • Expected space complexity : O(m*n)

You can see the full challenge with visuals at this link.

Challenges • Asked about 5 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Sep 25, 2019:

This is the main discussion thread generated for Climbing the Word Ladder.