forked from Adam-Jimenez/binarysearch-editorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary Nomad.py
More file actions
38 lines (36 loc) · 1.57 KB
/
Dictionary Nomad.py
File metadata and controls
38 lines (36 loc) · 1.57 KB
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
37
38
"""
Dictionary Nomad
The challenge in this problem is to efficiently build the graph representing the valid transitions between words. One way is to group words by strongly connected components, where every word in the same bucket share all letters except one, and then we can retrieve the neighbors of a word by retrieving all the strongly connected components they belong to.
When we have a way to get neighbors of a word, we can apply the classic BFS approach to find the shortest path in a non-weighted graph.
"""
from collections import defaultdict,deque
class Solution:
def solve(self, dictionary, start, end):
components=defaultdict(list)
neighbors=defaultdict(set)
l = len(dictionary[0])
for omit in range(l):
for w in dictionary:
key=(omit,w[:omit],w[omit+1:])
components[key].append(w)
def get_neighbors(word):
for omit in range(len(word)):
key=(omit,word[:omit],word[omit+1:])
if key in seen_components: continue
for nxt in components[key]:
if nxt not in seen and nxt != word:
yield nxt
seen_components.add(key)
q=deque([start])
seen=set([start])
seen_components=set()
ans=0
while q:
ans+=1
for _ in range(len(q)):
node=q.popleft()
if node == end: return ans
for nxt in get_neighbors(node):
seen.add(nxt)
q.append(nxt)
return -1