-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path127. Word Ladder.py
31 lines (28 loc) · 1.24 KB
/
127. 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
# Problem Statement: https://leetcode.com/problems/word-ladder/
class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
visited = set([beginWord])
wordSet = set(wordList)
startSet, endSet = set([beginWord]), set([endWord])
if endWord not in wordSet:
return 0
return self.bfs(startSet, endSet, visited, wordSet)
def bfs(self, startSet, endSet, visited, wordSet):
steps = 1
while startSet and endSet:
if len(startSet) > len(endSet):
startSet, endSet = endSet, startSet
steps += 1
nextSet = set()
for word in startSet:
for i in range(len(word)):
for ch in "abcdefghijklmnopqrstuvwxyz":
newWord = word[:i] + ch + word[i+1:]
if newWord in endSet:
return steps
if newWord in wordSet and newWord not in visited:
nextSet.add(newWord)
wordSet.remove(newWord)
visited.add(newWord)
startSet = nextSet
return 0