-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path127.py
33 lines (33 loc) · 1.07 KB
/
127.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
class Solution:
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""
if not (endWord in wordList):
return 0
wordList = set(wordList)
visited = set([beginWord])
if beginWord in wordList:
wordList.remove(beginWord)
dist = 1
while visited:
dist += 1
temp = []
for word in visited:
for i in range(len(word)):
chars = list(word)
for j in range(ord('a'), ord('z') + 1):
chars[i] = chr(j)
newWord = ''.join(chars)
if newWord == endWord:
return dist
if newWord in wordList:
wordList.remove(newWord)
temp.append(newWord)
if not temp:
return 0
visited = set(temp)
return dist