-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path127. Word Ladder.py
33 lines (27 loc) · 1.13 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
32
33
from collections import deque, defaultdict
class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
if endWord not in wordList:
return 0
wordList = set(wordList)
wordList.add(beginWord)
# Build the graph
neighbors = defaultdict(list)
for word in wordList:
for i in range(len(word)):
pattern = word[:i] + '*' + word[i+1:]
neighbors[pattern].append(word)
# BFS to find the shortest path
queue = deque([(beginWord, 1)])
visited = set([beginWord])
while queue:
current_word, level = queue.popleft()
for i in range(len(current_word)):
pattern = current_word[:i] + '*' + current_word[i+1:]
for neighbor in neighbors[pattern]:
if neighbor == endWord:
return level + 1
if neighbor not in visited:
visited.add(neighbor)
queue.append((neighbor, level + 1))
return 0