~/ learn/ comp-372/ cards/ DP & greedy problem clinic (design on a novel problem)
1 of 6

Type the edit-distance recurrence core (the else branch)

Type the edit-distance recurrence core (the else branch)

Answer

cost = 0 if s[i-1] == t[j-1] else 1 dp[i][j] = min( dp[i-1][j] + 1, # delete s[i] dp[i][j-1] + 1, # insert t[j] dp[i-1][j-1] + cost, # match / substitute )

Three predecessors feed each cell: up (delete), left (insert), diagonal (match if equal, else substitute at cost 1). That is the entire algorithm; the base row/column encode turning a prefix into the empty string.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/0f68ef17-ec20-42df-8554-d78c2a757f27/flashcard utf-8 LF