Hone logo
Hone
Problems

Word Ladder

Given two words, a starting word and an ending word, and a dictionary of valid words, find the shortest sequence of transformations from the starting word to the ending word. Each transformation must change only one letter at a time, and each transformed word must exist in the provided dictionary. This problem is a classic graph traversal scenario, useful for understanding search algorithms and string manipulation.

Problem Description

You are tasked with implementing a function that finds the shortest "word ladder" from a startWord to an endWord. A word ladder is a sequence of words where each adjacent pair of words differs by exactly one letter, and all words in the sequence (including startWord and endWord) are present in a given wordList. The goal is to find the sequence with the minimum number of transformations.

Requirements:

  • The function should return the length of the shortest word ladder. If no such ladder exists, return 0.
  • The startWord and endWord will have the same length.
  • All words in wordList will have the same length as startWord and endWord.
  • startWord is not necessarily in wordList. endWord is guaranteed to be in wordList.
  • You can assume that the wordList does not contain duplicate words.

Expected Behavior:

The function should explore possible transformations from the startWord, aiming to reach the endWord using the fewest steps.

Edge Cases:

  • If startWord and endWord are the same, the ladder length is 1.
  • If endWord is not reachable from startWord through valid one-letter transformations, the function should return 0.
  • The wordList might be empty.

Examples

Example 1:

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

Output: 5

Explanation:
A shortest word ladder is "hit" -> "hot" -> "dot" -> "dog" -> "cog", which has a length of 5.

Example 2:

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

Output: 0

Explanation:
"cog" is not in the wordList, therefore no ladder can be formed to reach it.

Example 3:

Input:
startWord = "a"
endWord = "c"
wordList = ["a", "b", "c"]

Output: 2

Explanation:
A shortest word ladder is "a" -> "c". (Note: "a" and "c" differ by one letter, and both are in the wordList).

Constraints

  • 1 <= startWord.length <= 10
  • endWord.length == startWord.length
  • 1 <= wordList.length <= 5000
  • endWord is in wordList.
  • All words in wordList have the same length as startWord.
  • All words consist of lowercase English letters.
  • There are no duplicates in wordList.
  • The solution should aim for reasonable performance, considering the input sizes.

Notes

  • This problem can be modeled as finding the shortest path in an unweighted graph. What are the nodes? What are the edges?
  • Consider using a breadth-first search (BFS) approach, as it's naturally suited for finding the shortest path in unweighted graphs.
  • You'll need an efficient way to determine if two words are "neighbors" (differ by exactly one letter).
  • Be mindful of how you manage visited words to avoid infinite loops and redundant computations.
Loading editor...
plaintext