Problem
A transformation sequence from word beginWord
to word endWord
using a dictionary wordList
is a sequence of words beginWord -> s1 -> s2 -> ... -> sk
such that:
- Every adjacent pair of words differs by a single letter.
- Every
si
for1 <= i <= k
is inwordList
. Note thatbeginWord
does not need to be inwordList
. sk == endWord
Given two words, beginWord
and endWord
, and a dictionary wordList
, return the number of words in the shortest transformation sequence from beginWord
to endWord
, or 0
if no such sequence exists.
OR
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
- only one letter can be changed at a time and
- each intermediate word must exist in the dictionary.
Examples
Example 1:
|
|
Example 2:
|
|
Note that we account for the length of the transformation path instead of the number of transformation itself.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
Solution
Method 1 - BFS
To find the shortest transformation sequence, we can perform a Breadth-First Search (BFS). BFS is suitable here as it explores all possible transformations level by level, ensuring that the first time we reach the endWord
, we have found the shortest path.
Approach
- Initial Checks: Ensure
endWord
is in thewordList
. If not, return 0. - BFS Initialization: Use a queue to store the current word and its transformation depth. Begin with the
beginWord
and a depth of 1. - BFS Execution:
- For each word, generate all possible single-letter transformations.
- For each transformation, if it matches the
endWord
, return the current depth + 1. - If the transformation is in
wordList
, add it to the queue and remove it fromwordList
to prevent revisiting.
- Return: If the queue is exhausted without finding the
endWord
, return 0.
graph TD; A["hit"] --> B["a̲it"]:::nonAns & C["b̲it"]:::nonAns & D["..."]:::nonAns & E["z̲it"]:::nonAns & F["ha̲t"]:::nonAns & G["..."]:::nonAns & H["ho̲t"]:::ans & I["..."]:::nonAns & J["hz̲t"]:::nonAns & K["..."]:::nonAns & L["hiz̲"]:::nonAns H ---> P["..."]:::nonAns & Q["d̲ot"]:::ans & R["..."]:::nonAns classDef ans fill:#fc6b03 classDef nonAns stroke-width:2px, stroke-dasharray: 5 5, font-size:10pt
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(M*N)
whereM
is the length of each word andN
is the total number of words in thewordList
, because generating all possible transformations and comparing them takes linear time. - 🧺 Space complexity:
O(M*N)
due to the space needed for the queue and the intermediate transformations.