Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 72 Edit Distance - State Design

Minimum cost

Define dp[i][j]dp[i][j] as the minimum operations to convert A[0..i-1] to B[0..j-1]. Base cases differ from LCS: dp[0][j]=jdp[0][j] = j: Converting empty string to B[0..j] requires jj insertions. dp[i][0]=idp[i][0] = i: Converting A[0..i] to empty string requires ii deletions.

These non-zero base cases reflect that converting to/from empty strings has a cost. Unlike LCS, base cases here are non-zero: converting to or from an empty string costs exactly the other string's length.