PythiloLesson 6 of 8 · progress kept in this browser

Reduce edit-distance memory to one row

Edit distance compares prefixes of two strings. Each cell depends on the cell above, the cell to its left and the old diagonal. Once a row is complete, earlier rows can be discarded. Keeping one previous row changes memory use from a full rectangle to a single line while preserving the same recurrence.

previous = [0, 1, 2]
current = [1]
print(min(previous[1] + 1, 2))
Your task

Write distance(a, b): the minimum number of single-character insertions, deletions and substitutions to turn a into b. Use rows rather than recursive enumeration.