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))Think before coding
The recurrence covers the possible final operations on two prefixes. Row i uses only row i minus one and its own left neighbour. Keeping just those rows gives quadratic time and linear space. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
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.
The explanation opens here once every check passes.