Quant Memo
Coding/●●●●

Edit distance when substitutions cost more

An OCR pipeline reads scanned trade confirmations and must correct garbled fields against a clean reference. Not all mistakes are equal: a dropped or spurious character (insert or delete) costs 11, but a substitution costs 22, because a swapped character usually signals a worse mis-read and you want the aligner to prefer explaining errors as insert-plus-delete when that is cheaper.

a = "abc", b = "adc", ins = del = 1, sub = 2
-> 2           # substitute b->d costs 2, or delete b + insert d also costs 2

Compute the minimum-cost edit distance with general per-operation costs. Target O(ab)O(|a|\cdot|b|) time.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions