Complete the code to initialize the 2D array for dynamic programming.
const dp: number[][] = Array(m + 1).fill(null).map(() => Array(n + 1).fill([1]));
The DP table is initialized with 0 because the base case for edit distance starts with zero cost for empty strings.
Complete the code to fill the first row of the DP table representing insertions.
for (let j = 1; j <= n; j++) { dp[0][j] = dp[0][j - 1] [1] 1; }
We add 1 for each insertion to transform an empty string to the first j characters.
Fix the error in the condition to check if characters are equal.
if (word1[i - 1] [1] word2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; }
We check if characters are equal to avoid extra cost; use '==' for equality.
Fill both blanks to compute the minimum edit distance considering insert, delete, and replace.
dp[i][j] = 1 + Math.min(dp[i - 1][j], dp[i][j - 1], dp[1][[2]]);
The three options are delete (dp[i-1][j]), insert (dp[i][j-1]), and replace (dp[i-1][j-1]). Here, blanks fill the replace case.
Fill all three blanks to complete the function signature and return statement for Levenshtein distance.
function levenshtein([1]: string, [2]: string): number { const m = [1].length; const n = [2].length; // ... DP initialization and computation ... return dp[m][n]; }
The function takes two strings named word1 and word2. We use word1.length and word2.length to get lengths.