LC 127 - Word Ladder
LC 127 - Word Ladder
Question
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
sifor1 <= i <= kis inwordList. Note thatbeginWorddoes 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.
Example 1:
1
2
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5 **Explanation:** One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
Example 2:
1
2
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output: 0 **Explanation:** The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
Constraints:
1 <= beginWord.length <= 10endWord.length == beginWord.length1 <= wordList.length <= 5000wordList[i].length == beginWord.lengthbeginWord,endWord, andwordList[i]consist of lowercase English letters.beginWord != endWord- All the words in
wordListare unique.
Links
Question here and solution here
Solution
concept
The key concept is to realise this is a graph problem. The most difficult part of the question is to construct the adjacency list. The normal way of doing it will cause TLE.
code
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
class Solution:
"""
graph + BFS
each node is a word, each edge is a transformation (single edit distance)
find the shortest path from beginWord to endWord
"""
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
if endWord not in wordList:
return 0
# create adjacency list: {pattern: [word]}
# e.g. {*it : [hit, hot]}
# use a wild card pattern such that this process is n*m^2
# if use double loop is is n^2*m which will TLE
neighbor_map = defaultdict(list)
wordList.append(beginWord) # wordList does not contain begin word
for word in wordList:
for j in range(len(word)):
pattern = word[:j] + "*" + word[j+1:]
neighbor_map[pattern].append(word)
# BFS
visited = set([beginWord])
q = deque([beginWord])
result = 1
while q:
for i in range(len(q)):
word = q.popleft()
if word == endWord:
return result
for j in range(len(word)):
pattern = word[:j] + "*" + word[j+1:]
for neighbor in neighbor_map[pattern]:
if neighbor not in visited:
visited.add(neighbor)
q.append(neighbor)
result += 1
return 0
Complexity
time: $O(n^2 \times m)$
space: $O(n^2)$
This post is licensed under CC BY 4.0 by the author.