Word Ladder

Leetcode - No. 127

Word Ladder

Word Ladder

Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

Only one letter can be changed at a time.
Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  • You may assume no duplicates in the word list.
  • You may assume beginWord and endWord are non-empty and are not the same.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Example 1:

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

Output: 5

Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Example 2:

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

Output: 0

Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.

Problem Analysis

Like a BFS Level Traversal problem, need to try every possible word of the current Words.

Algorithm Analysis

Find the shortest path, what I could think about is to use a BFS Algorithm. It likes tree structure problems, from the beginning word, I see it as a root node and its child will be all possible words that exist in the word list. So, to search the word in the list more efficiently, I will put all words into a map and its value will be used to record if I have already visited the word. Since if we meet the word that has been visited before in the previous level, which mean the current path might not be the shortest one.

Used a Queue to record all incoming string that need to be processed, and a step counter to record the current step, by level traverse all string, I first check if the end word is equal to the current word, if yes, it will return the current step, if not I will use a function to generate all possible words and check if that word is in the map and has not been visited, put that word into the queue and mark as visited.

Time Complexity Analysis

Time Complexity: (n26)^L

n = number of words in the list
L = length of each word in list

Code Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
Map<String, Integer> dict = new HashMap<>(); /* 0 - not visited, 1 visited */
Deque<String> queue = new ArrayDeque<>();
int step = 0;

for(String word : wordList) {
dict.put(word, 0);
}

if(beginWord.equals(endWord)) {
return 0;
}

if(!dict.containsKey(endWord)) {
return 0;
}

queue.offerLast(beginWord);
dict.put(beginWord, 1);


while(!queue.isEmpty()) {
step++;
int size = queue.size();
for(int i = 0; i < size; i++) {
String currentWord = queue.pollFirst();
if(currentWord.equals(endWord)) {
return step;
}
wordCheckHelper(endWord, currentWord, dict, queue);
}
}

return 0;
}

public void wordCheckHelper(String endWord, String currentWord, Map<String, Integer> dict, Deque<String> queue) {
for(int i = 0; i < currentWord.length(); i++) {
char[] currentCharWord = currentWord.toCharArray();
for(char c = 'a' ; c <= 'z'; c++) {
if(currentWord.charAt(i) == c) {
continue;
}
currentCharWord[i] = c;
String changedWord = String.valueOf(currentCharWord);
if(dict.containsKey(changedWord) && dict.get(changedWord) == 0) {
queue.offerLast(changedWord);
dict.put(changedWord, 1);
}
}
}
}
}