Problem
You are given two strings beginWord and endWord, and an array of strings wordList, where all words have the same length.Return the length of the shortest transformation sequence from beginWord to endWord, including both the starting and ending words. Each adjacent pair of words must differ by exactly one character, and every transformed word must exist in wordList.
If no such transformation sequence exists, return 0.
Example
Consider the following example to understand the expected input and output.Input
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
Output
5
The shortest transformation sequence is "hit" → "hot" → "dot" → "dog" → "cog". It contains 5 words, and each adjacent pair differs by exactly one character.
Solution
This solution uses the Breadth-First Search (BFS) technique along with a HashSet to find the shortest transformation sequence. Since BFS explores all words at the current level before moving to the next level, the first time we reach endWord, we have found the shortest sequence.The wordSet stores all words from wordList and allows efficient lookup. If endWord is not present in the set, we return 0 because the target cannot be reached.
We add beginWord to a queue and initialize the sequence length to 1. For each word removed from the queue, we change each character to every letter from 'a' to 'z' to generate possible transformations. If a transformed word exists in wordSet, we add it to the queue and remove it from the set to prevent revisiting it.
When a generated word matches endWord, we return the current sequence length plus one. If the queue becomes empty without reaching the target, we return 0.
class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
Set<String> wordSet = new HashSet<>(wordList);
if (!wordSet.contains(endWord)) return 0;
Queue<String> queue = new LinkedList<>();
queue.offer(beginWord);
wordSet.remove(beginWord);
int sequenceLength = 0;
while (!queue.isEmpty()) {
sequenceLength++;
int size = queue.size();
for (int i = 0; i < size; i++) {
String currentWord = queue.poll();
if (currentWord.equals(endWord)) return sequenceLength;
char[] wordArray = currentWord.toCharArray();
for (int j = 0; j < wordArray.length; j++) {
char originalChar = wordArray[j];
for (char replacementChar = 'a'; replacementChar <= 'z'; replacementChar++) {
if (replacementChar == originalChar) continue;
wordArray[j] = replacementChar;
String nextWord = new String(wordArray);
if (wordSet.contains(nextWord)) {
queue.offer(nextWord);
wordSet.remove(nextWord);
}
wordArray[j] = originalChar;
}
}
}
}
return 0;
}
}
Complexity
Let N be the number of words in wordList and L be the length of each word. Each word is processed at most once. For every character, we try up to 26 replacements, and creating and hashing each generated string takes O(L) time.Therefore, the overall time complexity is O(N × L²).
The HashSet and BFS queue can each store up to N words of length L. Therefore, the extra space complexity is O(N × L).
Comments
No comments yet. Ask a question or share what helped.