Graphs
114 / 170GraphsLesson 7 of 8About 54 min
The nodes are states and the edges are moves. Nothing is stored; the neighbors are computed on demand.
The graph is never built. A word's neighbors are produced on demand by changing one character at a time and keeping whatever the dictionary contains. Type the two lines it prints.
from string import ascii_lowercase words = {"hot", "dot", "dog", "lot", "log", "cog"}word = "hot" neighbors = []for index in range(len(word)): for letter in ascii_lowercase: candidate = word[:index] + letter + word[index + 1:] if candidate != word and candidate in words: neighbors.append(candidate) print(sorted(neighbors))print("candidates built to find them:", len(word) * len(ascii_lowercase))Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.