Overview
Count the fewest inserts, deletes and replacements between two words with a table of smaller answers, the idea behind "did you mean…?" suggestions.
- d[i][j] for the first i and j letters
- insert, delete or replace
- row 0 and column 0 are not zeros
Short Course
Edit distance counts the fewest inserts, deletes and replacements that turn
one word into another. d[i][j] holds the answer for the first i letters
of one and j of the other. Trap: row 0 and column 0 aren't zeros; turning
"abc" into "" takes 3 deletes.
a, b = "cat", "cut"
rows, cols = len(a) + 1, len(b) + 1
d = [[0] * cols for _ in range(rows)]
for i in range(rows):
d[i][0] = i # delete i letters
Task
Write edit_distance(a, b): the fewest single-letter inserts, deletes and
replacements that turn a into b. Example:
edit_distance('kitten', 'sitting') → 3.
Test Suites
Pending
Next
Output