-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathstep_word_chain.py
33 lines (25 loc) · 988 Bytes
/
step_word_chain.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
'''
Given a start wor, an end word and a dictionary of same length valid words, find the shortest transformation sequence from start to end such that only one letter is changed at each step of the sequence, and each transformed word exists in the dictionary else return none.
'''
from collections import deque
from string import ascii_lowercase
def word_ladder(start, end, words):
queue = deque([(start, [start])])
while queue:
word, path = queue.popleft()
if word == end:
return path
for i in range(len(word)):
for char in ascii_lowercase:
next_word = word[:i] + char + word[i+1:]
if next_word in words:
words.remove(next_word)
queue.append([next_word, path+[next_word]])
return None
start, end = "dog", "cat"
dictionary = ["dot", "dop", "dat", "cat"]
print(word_ladder(start, end, dictionary))
'''
Output :
['dog', 'dot', 'dat', 'cat']
'''