Shortest word ladder
Return the number of words in a shortest transformation from start to end, changing one character at a time through a supplied word list. All words have equal length. Return zero if impossible; start=end returns one.
a, b = 'hit', 'hot'
print(sum(x != y for x,y in zip(a,b)))Reason about the state
Breadth-first search finds the shortest unweighted path. Remove a word from the unvisited set when enqueuing it so repeated routes do not expand it again. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the number of words in a shortest transformation from start to end, changing one character at a time through a supplied word list. All words have equal length. Return zero if impossible; start=end returns one.
The explanation opens here once every check passes.