
Sponsored
Sponsored
This approach uses a 2D array to store the edit distances between all prefixes of both strings. By building solutions for smaller subproblems, we can efficiently calculate the solution for the entire problem.
We define dp[i][j] as the edit distance between the first i characters of word1 and the first j characters of word2. The recursion is based on comparing the last characters of the current substrings. If they are the same, the edit distance is the same as dp[i-1][j-1], otherwise, we add 1 (to account for an edit operation) and consider the minimum edit distance found after each possible operation: insertion, deletion, or replacement.
Time Complexity: O(m * n) where m and n are the lengths of word1 and word2 respectively, as we compute values for each combination of indices.
Space Complexity: O(m * n) for storing the dp table.
1function minDistance(word1, word2) {
2 const m = word1.length;
3 const n = word2.length;
4 const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
5
6 for (let i = 0; i <= m; ++i) {
7 dp[i][0] = i;
8 }
9 for (let j = 0; j <= n; ++j) {
10 dp[0][j] = j;
11 }
12
13 for (let i = 1; i <= m; ++i) {
14 for (let j = 1; j <= n; ++j) {
15 if (word1[i - 1] === word2[j - 1]) {
16 dp[i][j] = dp[i - 1][j - 1];
17 } else {
18 dp[i][j] = 1 + Math.min(
19 dp[i - 1][j], // delete
20 dp[i][j - 1], // insert
21 dp[i - 1][j - 1] // replace
22 );
23 }
24 }
25 }
26
27 return dp[m][n];
28}This JavaScript function mirrors the dynamic programming approach used in the Python solution. A 2D array dp is created to store the edit distances.
We iterate over the lengths of the strings, filling out the dp table according to whether characters match or not, updating the distances according to delete, insert, and replace operations.
This approach uses recursive calls with memoization to effectively reduce the factors of redundant subproblem calculations. We use a hash map or an array to store results of previously encountered subproblems to avoid recalculating them, subsequently turning the recursive depth-first search into a memoized search.
Time Complexity: O(m * n) where m and n are lengths of word1 and word2. Each subproblem is computed once.
Space Complexity: O(m * n) for memoization storage plus recursion call stack space.
1using System;
2using System.Collections.Generic;
3
4public class Solution {
5 private Dictionary<(int, int), int> memo = new Dictionary<(int, int), int>();
public int MinDistance(string word1, string word2) {
return MinDistanceHelper(word1, word2, word1.Length, word2.Length);
}
private int MinDistanceHelper(string word1, string word2, int m, int n) {
if (memo.ContainsKey((m, n))) {
return memo[(m, n)];
}
if (m == 0) return n;
if (n == 0) return m;
if (word1[m - 1] == word2[n - 1]) {
memo[(m, n)] = MinDistanceHelper(word1, word2, m - 1, n - 1);
} else {
int insert = MinDistanceHelper(word1, word2, m, n - 1);
int delete = MinDistanceHelper(word1, word2, m - 1, n);
int replace = MinDistanceHelper(word1, word2, m - 1, n - 1);
memo[(m, n)] = 1 + Math.Min(insert, Math.Min(delete, replace));
}
return memo[(m, n)];
}
}The C# solution uses recursion with a Dictionary for memoization. The approach efficiently prevents duplicate calculations by storing previously computed edit distances as key-value pairs, where the key is a tuple of current substring indices.
By breaking the problem into subproblems based on recursive function calls, it ensures that computed data is reused whenever the same indices are encountered.