-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path127_Word_Ladder.py
More file actions
22 lines (22 loc) · 873 Bytes
/
Copy path127_Word_Ladder.py
File metadata and controls
22 lines (22 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 2015-09-30 Runtime: 456 ms
class Solution(object):
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: Set[str]
:rtype: int
"""
# queue will store (word, number of transitions)
queue, wordLen = collections.deque([(beginWord, 1)]), len(beginWord)
while queue:
w = queue.popleft()
for i in xrange(wordLen):
firstHalf, secondHalf = w[0][:i], w[0][i + 1:]
for ch in 'abcdefghijklmnopqrstuvwxyz':
newWord = firstHalf + ch + secondHalf
if newWord == endWord: return w[1] + 1
if newWord in wordList:
queue.append((newWord, w[1] + 1))
wordList.remove(newWord)
return 0