At position , compare A[i-1] and B[j-1]. If they match: . No operation needed. If they don't match: That's delete, insert, and replace respectively.
Each costs 1. The final answer is . The formula is the core of any DP solution. It defines how dp[i][j] depends on earlier dp values. Getting this formula right is often the hardest and most rewarding part.