-
Notifications
You must be signed in to change notification settings - Fork 0
/
word-ladder.py
65 lines (58 loc) · 2.14 KB
/
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
class Solution(object):
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""
distance = 0
current = [beginWord]
visited = [beginWord]
d = {}
for word in wordList:
for i in range(len(word)):
bucket = word[:i] + '_' + word[i + 1:]
if bucket in d:
d[bucket].append(word)
else:
d[bucket] = [word]
while current:
# queue
_next = []
for word in current:
if word == endWord:
return distance + 1
for i in range(len(word)):
b = word[:i] + '_' + word[i + 1:]
if b in d:
for c in d[b]:
if c not in visited:
_next.append(c)
visited.append(c)
distance += 1
current = _next
return 0
import collections
class Solution2(object):
def ladderLength(self, beginWord, endWord, wordList):
w = set(wordList)
# w.add(endWord)
queue = collections.deque([[beginWord, 1]])
print queue
while queue:
word, length = queue.popleft()
if word == endWord:
return length
for i in range(len(word)):
for c in 'abcdefghijklmnopqrstuvwxyz':
next_word = word[:i] + c + word[i+1:]
if next_word in w:
w.remove(next_word)
queue.append([next_word, length + 1])
return 0
if __name__ == "__main__":
# print Solution().ladderLength("hit", "cog", ["hot", "dot", "dog", "lot", "log"])
# print Solution().ladderLength("hit", "cog", ["hot", "dot", "dog", "lot", "log", "cog"])
print Solution2().ladderLength("hit", "cog", ["hot", "dot", "dog", "lot", "log", "cog"])
# print Solution2().ladderLength("hit", "cog", ["hot", "dot", "dog", "lot", "log"])