Dynamic Programming21 sections · 916 units
Open in Course

Edit Distance - State Definition

Prefix to prefix

Define dp[i][j]dp[i][j] = minimum operations to convert the first ii characters of A into the first jj characters of BB. The answer is dp[len(A)][len(B)]dp[len(A)][len(B)].

This "prefix to prefix" pattern (comparing beginnings of both strings) is the foundation of all string DP. The state must capture everything needed to solve the subproblem. If you are missing information, you cannot compute the answer correctly. If you include too much, your solution will be slow or use too much memory.