An interactive visualization of how dynamic programming finds the minimum number of operations to convert one string to another. Watch the algorithm fill the DP table and compute the edit distance.
Dynamic Programming LeetCode 72 ↗ Mediumword1 and word2, return the minimum number of operations required to convert word1 to word2.fun minDistance(word1: String, word2: String): Int { val m = word1.length; val n = word2.length val dp = Array(m + 1) { IntArray(n + 1) } for (i in 0..m) dp[i][0] = i // delete all for (j in 0..n) dp[0][j] = j // insert all for (i in 1..m) { for (j in 1..n) { if (word1[i - 1] == word2[j - 1]) dp[i][j] = dp[i - 1][j - 1] else dp[i][j] = 1 + minOf( dp[i - 1][j], // delete dp[i][j - 1], // insert dp[i - 1][j - 1] // replace ) } } return dp[m][n]}
The Edit Distance problem (also known as Levenshtein Distance) uses dynamic programming to build up the solution.
We create a 2D table where dp[i][j] represents the minimum number of operations needed to convert
the first i characters of word1 to the first j characters of word2.
word1. Cost: 1 + dp[i][j-1]word1. Cost: 1 + dp[i-1][j]word1. Cost: 1 + dp[i-1][j-1]dp[i-1][j-1]
Base cases: dp[i][0] = i (delete all i characters) and dp[0][j] = j (insert all j characters).
For each cell dp[i][j]:
• If word1[i-1] == word2[j-1]: dp[i][j] = dp[i-1][j-1]
• Otherwise: dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])
We fill a table of size (m+1) × (n+1) where m and n are the lengths
of the two strings. Each cell is computed in constant time by looking at three neighboring cells.
The space complexity can be optimized to O(min(m, n)) by only keeping two rows at a time.