-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path0127.py
36 lines (29 loc) · 1023 Bytes
/
0127.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
class Solution:
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""
wordList.append(endWord)
q = list()
q.append([beginWord, 0])
if beginWord in wordList:
wordList.remove(beginWord)
while any(q):
tmpStr, step = q.pop(0)
if tmpStr == endWord:
return step
for i in range(len(tmpStr)):
_tmpStr = tmpStr
for i in range(26):
_tmpStr[i] = str(chr(97 + i))
if wordList.count(_tmpStr) == 1:
q.append([_tmpStr, step + 1])
wordList.remove(_tmpStr)
if __name__ == '__main__':
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
print(Solution().ladderLength(beginWord, endWord, wordList))