FastPrepFastPrep
Problem Brief

Determine the Edit Distance in a Word Ladder

OA

Given two words (beginWord and endWord), and a dictionary's word list, find the minimum number of operations needed to change beginWord into endWord.

You can change only one letter at a time, and each intermediate word must exist in the word list. If there is no possible transformation, return None (Python), -1 (Java & C++), null (Javascript).

1Example 1

Input
beginWord = "hit", endWord = "cog", wordList = ["hit", "hot", "dot", "dog", "cog"]
Output
4
Explanation

The transformation sequence can be: hit -> hot -> dot -> dog -> cog, which is 4 steps long.

2Example 2

Input
beginWord = "word", endWord = "word", wordList = ["word", "ward"]
Output
0
Explanation

Since the beginWord is the same as the endWord, no transformation is needed, so the number of steps is 0.

3Example 3

Input
beginWord = "hit", endWord = "cog", wordList = ["hit", "hot", "dot", "dog"]
Output
-1
Explanation

The word cog is not in the list, so there is no possible transformation sequence.

public int ladderLength(String beginWord, String endWord, List<String> wordList) {
  // write your code here
}
Input

beginWord

"hit"

endWord

"cog"

wordList

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

Output

4

Sign in to submit your solution.