Dynamic Programming21 sections · 916 units
Open in Course

Edit Distance - Base Cases

Empty strings

Converting empty A to BB[1..jj]: Insert all jj characters. dp[0][j]=jdp[0][j] = j. Converting AA[1..ii] to empty BB: Delete all ii characters. dp[i][0]=idp[i][0] = i.

Both empty: dp[0][0]=0dp[0][0] = 0. These base cases fill the first row and column. Base cases are where the recursion stops. They handle the simplest possible inputs directly, without making recursive calls. Every recursive function needs at least one base case, or it will recurse forever and crash.