What is the Edit Distance (Levenshtein distance) between two strings?
It is the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into the other.
Click to reveal answer
beginner
Which operations are allowed in calculating the Levenshtein distance?
Insertion, deletion, and substitution of a single character.
Click to reveal answer
intermediate
What is the base case when using dynamic programming for the Levenshtein distance?
When one string is empty, the distance is the length of the other string (all insertions or deletions).
Click to reveal answer
intermediate
How does the dynamic programming table for Levenshtein distance get filled?
Each cell represents the minimum edit distance between prefixes of the two strings, calculated using previous results for insert, delete, and substitute operations.
Click to reveal answer
intermediate
What is the time complexity of the standard dynamic programming solution for Levenshtein distance?
O(m * n), where m and n are the lengths of the two strings.
Click to reveal answer
Which of the following is NOT an allowed operation in Levenshtein distance?
AInsertion
BTransposition
CSubstitution
DDeletion
✗ Incorrect
Levenshtein distance allows insertion, deletion, and substitution. Transposition is not allowed.
If one string is empty and the other has length 5, what is the edit distance?
A0
B1
CCannot determine
D5
✗ Incorrect
The distance equals the length of the non-empty string because all characters must be inserted or deleted.
What does the cell dp[i][j] represent in the dynamic programming table for Levenshtein distance?
ADistance between prefixes of length i and j
BNumber of substitutions only
CDistance between full strings
DLength of the longest common subsequence
✗ Incorrect
dp[i][j] stores the minimum edit distance between the first i characters of string one and first j characters of string two.
What is the time complexity of the Levenshtein distance dynamic programming solution?
AO(m^2 + n^2)
BO(m + n)
CO(m * n)
DO(2^(m+n))
✗ Incorrect
The solution fills a table of size m by n, so the time complexity is O(m * n).
Which of these best describes the Levenshtein distance?
AMinimum number of edits to convert one string to another
BLongest common subsequence length
CNumber of matching characters
DMaximum number of substitutions
✗ Incorrect
Levenshtein distance measures the minimum edits (insertions, deletions, substitutions) to convert one string into another.
Explain how to use dynamic programming to find the Levenshtein distance between two strings.
Think about comparing prefixes and building up solutions.
You got /4 concepts.
Describe the three operations allowed in the Edit Distance problem and how they affect the distance calculation.
Imagine changing one word into another step by step.
You got /4 concepts.
Practice
(1/5)
1. What does the Edit Distance (Levenshtein distance) between two strings measure?
easy
A. The minimum number of insertions, deletions, or substitutions to convert one string into another
B. The number of matching characters in the same positions
C. The length difference between two strings
D. The maximum number of characters that differ between two strings
Solution
Step 1: Understand the definition of Edit Distance
Edit Distance counts the smallest number of changes needed to turn one string into another.
Step 2: Identify allowed operations
These operations include insertion, deletion, and substitution of characters.
Final Answer:
The minimum number of insertions, deletions, or substitutions to convert one string into another -> Option A
Quick Check:
Edit Distance = minimum edits needed [OK]
Hint: Think of edits needed to match strings exactly [OK]
Common Mistakes:
Confusing edit distance with matching characters count
Thinking it only counts substitutions
Assuming it measures length difference only
2. Which of the following is the correct way to initialize the first row of the DP table for Levenshtein distance in C?
easy
A. for (int j = 0; j <= n; j++) dp[0][j] = j;
B. for (int i = 0; i <= m; i++) dp[i][0] = i;
C. for (int j = 1; j <= n; j++) dp[0][j] = 0;
D. for (int i = 1; i <= m; i++) dp[0][i] = i;
Solution
Step 1: Identify the DP table dimensions
The DP table has dimensions (m+1) x (n+1), where m and n are string lengths.
Step 2: Initialize first row correctly
The first row dp[0][j] represents converting empty string to prefix of second string, so dp[0][j] = j.
Final Answer:
for (int j = 0; j <= n; j++) dp[0][j] = j; -> Option A
Quick Check:
First row = 0 to n [OK]
Hint: First row = indices of second string length [OK]
Common Mistakes:
Mixing row and column indices
Starting loop from 1 instead of 0
Initializing first column instead of first row
3. What is the output of the following C code snippet computing Levenshtein distance between "kitten" and "sitting"?
int min(int a, int b, int c) {
int m = a < b ? a : b;
return m < c ? m : c;
}
int levenshtein(char *s, char *t) {
int m = strlen(s), n = strlen(t);
int dp[m+1][n+1];
for (int i = 0; i <= m; i++) dp[i][0] = i;
for (int j = 0; j <= n; j++) dp[0][j] = j;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
int cost = (s[i-1] == t[j-1]) ? 0 : 1;
dp[i][j] = min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + cost);
}
}
return dp[m][n];
}
printf("%d", levenshtein("kitten", "sitting"));
medium
A. 3
B. 4
C. 5
D. 6
Solution
Step 1: Understand the strings and operations
"kitten" to "sitting" requires substitutions and insertions: k->s, e->i, add g.
Step 2: Calculate minimum edits
Minimum edits are 3 substitutions and 1 insertion = 4 total edits.
4. Identify the bug in this C code snippet for Levenshtein distance:
int levenshtein(char *s, char *t) {
int m = strlen(s), n = strlen(t);
int dp[m][n];
for (int i = 0; i <= m; i++) dp[i][0] = i;
for (int j = 0; j <= n; j++) dp[0][j] = j;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
int cost = (s[i-1] == t[j-1]) ? 0 : 1;
dp[i][j] = min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + cost);
}
}
return dp[m][n];
}
medium
A. Loops should start from 0, not 1
B. The cost calculation is incorrect
C. dp array size should be (m+1) x (n+1), not m x n
D. Return statement should be dp[m-1][n-1]
Solution
Step 1: Check dp array declaration
dp is declared as dp[m][n], but indices go up to m and n inclusive, so size is insufficient.
Step 2: Understand indexing needs
We need dp of size (m+1) x (n+1) to store base cases and full computation.
Final Answer:
dp array size should be (m+1) x (n+1), not m x n -> Option C
Quick Check:
dp size = (m+1)*(n+1) [OK]
Hint: DP table size must cover all indices including zero [OK]
Common Mistakes:
Declaring dp too small causing out-of-bounds errors
Misunderstanding loop ranges
Incorrect return index
5. Given two strings "intention" and "execution", what is the minimum edit distance using Levenshtein algorithm? Consider insertions, deletions, and substitutions only.
hard
A. 7
B. 8
C. 5
D. 6
Solution
Step 1: Analyze string lengths and differences
"intention" and "execution" differ in several characters and positions.