Word Ladder I


Given two words A and B, and a dictionary, C, find the length of shortest transformation sequence from A to B, such that:

  • You must change exactly one character in every transformation.
  • Each intermediate word must exist in the dictionary.

Note:

  1. Return 0 if there is no such transformation sequence.
  2. All words have the same length.
  3. All words contain only lowercase alphabetic characters.


Input Format:

The first argument of input contains a string, A.
The second argument of input contains a string, B.
The third argument of input contains an array of strings, C.

Output Format:

Return an integer representing the minimum number of steps required to change string A to string B.

Constraints:

1 <= length(A), length(B), length(C[i]) <= 25
1 <= length(C) <= 5e3

Example :

Input 1:
    A = "hit"
    B = "cog"
    C = ["hot", "dot", "dog", "lot", "log"]

Output 1:
    5

Explanation 1:
    "hit" -> "hot" -> "dot" -> "dog" -> "cog"
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Word Ladder I on Interview Code Editor
Sign Up
to access hints and editorial solutions for Word Ladder I
Asked In:

Discussion


Loading...
Click here to start solving coding interview questions