-
Notifications
You must be signed in to change notification settings - Fork 24
/
127 - Word Ladder.py
172 lines (163 loc) · 6.69 KB
/
127 - Word Ladder.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Solution #1: This is the first solution I came up with,
# it involves making a graph and then performing a BFS on the graph.
# While this solution times out on LeetCode, it's a great starting
# point for understanding how to solve the problem.
class Node(object):
def __init__(self):
self.adjacentNodes = []
class Solution(object):
# Returns True if a and b are one char apart, false otherwise
def oneCharDiff(self, a, b):
numDifferences = 0
for i in range(0, len(a)):
if a[i] != b[i]:
numDifferences += 1
if numDifferences > 1:
return False
return True
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""
# Make a graph where each word is a node. Words that are
# only one character apart are connected by edge.
wordList.append(beginWord)
graph = {}
for i in range(0, len(wordList)):
graph[wordList[i]] = Node()
for i in range(0, len(wordList)):
for j in range(i+1, len(wordList)):
if(self.oneCharDiff(wordList[i], wordList[j])):
graph[wordList[i]].adjacentNodes.append(wordList[j])
graph[wordList[j]].adjacentNodes.append(wordList[i])
# Apply BFS
distance = {} # distance[word] = # of nodes to reach word
distance[beginWord] = 1
queue = [beginWord]
while len(queue) != 0:
# Pop front of queue
front = queue.pop(0)
# Add adjacent nodes to the queue
for i in range(0, len(graph[front].adjacentNodes)):
if graph[front].adjacentNodes[i] not in distance:
distance[graph[front].adjacentNodes[i]] = distance[front] + 1
queue.append(graph[front].adjacentNodes[i])
if endWord not in distance:
return 0
else:
return distance[endWord]
# Solution #2: Let's make an optimization on the first solution.
# Note that constructing the graph takes O(V^2) time where
# V is number of nodes (words) in the graph. We can eliminate this cost
# by "making" the graph as we do the BFS. For example, the only
# nodes that could be connected to "hit" are a-z + i + t,
# h + a-z + t or h + i + a-z. This takes only 81 iterations
# to generate all possible children for each node we visit in the BFS.
# We could say this takes constant time with respect to the input,
# and we completely eliminate the cost of constructing the graph up front.
class Solution(object):
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""
wordDict = {}
for word in wordList:
wordDict[word] = True
# Apply BFS
distance = {} # distance[word] = # of nodes to reach word
distance[beginWord] = 1
queue = [beginWord]
while len(queue) != 0:
# Pop front of queue
front = queue.pop(0)
# Find all children of front by generating all possible
# children and checking if any of them exist
children = []
for i in range(0, len(front)):
for c in 'abcdefghijklmnopqrstuvwxyz':
possibleChild = front[:i] + c + front[i+1:]
if possibleChild in wordDict:
children.append(possibleChild)
# Add adjacent nodes to the queue
for i in range(0, len(children)):
if children[i] not in distance:
distance[children[i]] = distance[front] + 1
queue.append(children[i])
if endWord not in distance:
return 0
else:
return distance[endWord]
# Solution #3 (unidirectional BFS with sets):
# We can further optimize the previous solution by
# doing a bidirectional BFS. To set up for this, start by changing
# our BFS to use sets. The reason we need to use sets is because
# bidirectional BFS requires us to compare beginQueue with
# endQueue on each iteration. Sets are much faster to compare than lists.
# See: https://wiki.python.org/moin/TimeComplexity
class Solution(object):
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""
# Apply BFS with sets.
wordSet = set(wordList) # O(n) time to make set
front = set([beginWord])
distance = 1
while len(front) != 0:
# Generate all possible children of the nodes in front
possible = set(word[:i] + c + word[i+1:] for word in front for i in range(0, len(word)) for c in 'abcdefghijklmnopqrstuvwxyz')
# Take the intersection with wordSet to get the next children
# we want to visit
front = wordSet & possible
distance += 1
wordSet -= front
if endWord in front:
return distance
return 0
# Solution #4 (bidirectional BFS with): This is much faster than our
# previous solutions since it applies BFS in both directions.
class Solution(object):
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""
wordSet = set(wordList)
back = set([endWord])
front = set([beginWord])
distance = 1
# Edge case
if endWord not in wordSet:
return 0
# Apply bidirectional BFS
while len(front) != 0:
# Take the intersection of sets to see if we've found a
# common node. We need to do this at the start of the loop
# in case back = front.
if front & back:
return distance
# Remove nodes we've already visited to avoid a cycle
wordSet -= front
# Generate all possible children of the nodes in front
possible = set(word[:i] + c + word[i+1:] for word in front for i in range(0, len(word)) for c in 'abcdefghijklmnopqrstuvwxyz')
front = wordSet & possible
distance += 1
# Swap back and front. This is so on the next iteration
# we're expanding from the destination, and the iteration after
# after we're expanding from the source, and so on.
# This is the key part of bidirectional BFS.
tmp = front
front = back
back = tmp
return 0