0
0
DSA Typescriptprogramming~20 mins

Edit Distance Problem Levenshtein in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Levenshtein Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Levenshtein Distance Calculation
What is the output of the following TypeScript code that calculates the Levenshtein distance between two strings?
DSA Typescript
function levenshtein(a: string, b: string): number {
  const dp: number[][] = [];
  for (let i = 0; i <= a.length; i++) {
    dp[i] = [];
    dp[i][0] = i;
  }
  for (let j = 0; j <= b.length; j++) {
    dp[0][j] = j;
  }
  for (let i = 1; i <= a.length; i++) {
    for (let j = 1; j <= b.length; j++) {
      if (a[i - 1] === b[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1];
      } else {
        dp[i][j] = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1);
      }
    }
  }
  return dp[a.length][b.length];
}

console.log(levenshtein("kitten", "sitting"));
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
Think about the minimum number of edits (insertions, deletions, substitutions) to convert 'kitten' to 'sitting'.
🧠 Conceptual
intermediate
1:30remaining
Understanding Levenshtein Distance Matrix Initialization
In the Levenshtein distance algorithm, why do we initialize the first row and first column of the matrix with incremental values?
AThey are placeholders and do not affect the final result.
BThey store the maximum possible edit distance between the two strings.
CThey represent the cost of converting an empty string to the other string by insertions or deletions.
DThey represent the number of substitutions needed to match the strings.
Attempts:
2 left
💡 Hint
Think about how many edits it takes to get from an empty string to a string of length n.
🔧 Debug
advanced
2:00remaining
Identify the Error in Levenshtein Distance Implementation
What error will the following TypeScript code produce when calculating the Levenshtein distance?
DSA Typescript
function levenshtein(a: string, b: string): number {
  const dp: number[][] = [];
  for (let i = 0; i < a.length; i++) {
    dp[i] = [];
    dp[i][0] = i;
  }
  for (let j = 0; j <= b.length; j++) {
    dp[0][j] = j;
  }
  for (let i = 1; i <= a.length; i++) {
    for (let j = 1; j <= b.length; j++) {
      if (a[i - 1] === b[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1];
      } else {
        dp[i][j] = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1);
      }
    }
  }
  return dp[a.length][b.length];
}

console.log(levenshtein("abc", "yabd"));
ATypeError: Cannot set property '0' of undefined
BSyntaxError due to missing semicolon
CReturns incorrect distance value 2
DReturns correct distance value 3
Attempts:
2 left
💡 Hint
Check how the dp array is initialized for the first row and column.
🚀 Application
advanced
1:30remaining
Minimum Edit Operations to Convert Strings
Given the strings 'intention' and 'execution', what is the minimum number of edit operations (insertions, deletions, substitutions) needed to convert one into the other?
A4
B6
C7
D5
Attempts:
2 left
💡 Hint
Try to count the edits step-by-step or use the Levenshtein distance logic.
🧠 Conceptual
expert
1:00remaining
Time Complexity of Levenshtein Distance Algorithm
What is the time complexity of the classic dynamic programming solution for the Levenshtein distance between two strings of lengths m and n?
AO(2^(m + n))
BO(m * n)
CO(m^2 + n^2)
DO(m + n)
Attempts:
2 left
💡 Hint
Consider the size of the matrix filled during the algorithm.