Dynamic Programming21 sections · 916 units
Open in Course

Challenge: Edit Distance Reconstruction

Finding the operations

The DP gives the minimum count, but which operations? Backtrack from dp[m][n]dp[m][n]. At each cell, check which case gave the minimum: diagonal (match or replace), left (insert), up (delete).

If dp[i][j]=dp[i1][j1]dp[i][j] = dp[i-1][j-1] and chars match: no operation. If dp[i][j]=dp[i1][j1]+1dp[i][j] = dp[i-1][j-1] + 1: replace. If dp[i][j]=dp[i1][j]+1dp[i][j] = dp[i-1][j] + 1: delete from s1s_1. If dp[i][j]=dp[i][j1]+1dp[i][j] = dp[i][j-1] + 1: insert into s1s_1. When ties occur between operations, prefer the one that gives a more natural edit sequence.