Dynamic Programming21 sections · 916 units
Open in Course

Edit Distance - Transition

Match or operate

At position (i,j)(i,j), compare A[i]A[i] with B[j]B[j]: If they match: No operation needed. dp[i][j]=dp[i1][j1]dp[i][j] = dp[i-1][j-1]. If they don't match, choose the cheapest:

• Replace A[i]A[i] with B[j]B[j]: dp[i1][j1]+1dp[i-1][j-1] + 1 • Delete A[i]A[i]: dp[i1][j]+1dp[i-1][j] + 1 • Insert B[j]B[j]: dp[i][j1]+1dp[i][j-1] + 1 The transition is the core of any DP solution. All 33 operations pull from previously computed cells, so filling the table row by row produces correct results.