D1 = defaultdict(set)
D2 = defaultdict(set)
D3 = defaultdict(set)
for w in WORDS:
    D1[w[:2]].add(w)
D2[w[0] + w[2]].add(w)
D3[w[1:]].add(w)

def follows(w):
    followers = set(D1.get(w[:2]).union(D2.get(w[0] + w[2]), D3.get(w[1:])))
followers.discard(w)
return followers
for w in WORDS:
    pass
Breadth first search with three-letter words, optimization