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 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 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, computed using values from neighboring cells (top, left, and diagonal).
Click to reveal answer
beginner
Why is Levenshtein distance useful in real life?
It helps in spell checking, DNA sequence analysis, and natural language processing by measuring how similar two strings are.
Click to reveal answer
Which of the following is NOT an allowed operation in Levenshtein distance?
ATransposition
BDeletion
CInsertion
DSubstitution
✗ Incorrect
Levenshtein distance allows insertion, deletion, and substitution but not transposition (swapping two adjacent characters).
What is the Levenshtein distance between "cat" and "cut"?
A3
B2
C1
D0
✗ Incorrect
Only one substitution (a -> u) is needed, so the distance is 1.
If one string is empty and the other has length 5, what is the edit distance?
A0
B5
CCannot determine
D1
✗ Incorrect
You need 5 insertions or deletions to convert an empty string to a string of length 5.
In dynamic programming for Levenshtein distance, what does the cell dp[i][j] represent?
ADistance between prefixes of length i and j
BDistance between full strings
CNumber of substitutions only
DLength of the longer string
✗ Incorrect
dp[i][j] stores the minimum edit distance between the first i characters of one string and the first j characters of the other.
Which algorithmic technique is commonly used to solve the Levenshtein distance problem efficiently?
AGreedy algorithm
BBacktracking
CDivide and conquer
DDynamic programming
✗ Incorrect
Dynamic programming efficiently computes the minimum edit distance by building solutions to smaller subproblems.
Explain how dynamic programming helps in calculating the Levenshtein distance between two strings.
Think about how you compare prefixes of the strings step by step.
You got /4 concepts.
Describe a real-life scenario where calculating the Levenshtein distance is useful and why.
Consider situations where you want to find how close two words or sequences are.
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 how many changes are needed to make two strings equal.
Step 2: Identify allowed operations
Allowed operations are 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
Ignoring substitution as an allowed operation
Thinking it measures only length difference
2. Which of the following is the correct way to initialize the base case for the DP table in Levenshtein distance calculation in C++?
easy
A. for (int i = 0; i <= m; i++) dp[i][0] = i;
B. for (int j = 0; j <= n; j++) dp[0][j] = 0;
C. dp[0][0] = 1;
D. for (int i = 1; i <= m; i++) dp[i][0] = 0;
Solution
Step 1: Understand base case meaning
dp[i][0] means converting first i characters of string1 to empty string, so requires i deletions.
Step 2: Identify correct initialization
Setting dp[i][0] = i correctly counts deletions needed for empty second string.
Final Answer:
for (int i = 0; i <= m; i++) dp[i][0] = i; -> Option A
Quick Check:
Base case rows = index count [OK]
Hint: Base case dp[i][0] = i for deletions [OK]
Common Mistakes:
Initializing dp[0][j] incorrectly to zero
Starting loops from 1 instead of 0
Setting dp[0][0] to 1 instead of 0
3. What is the output of the following C++ code snippet that calculates the Levenshtein distance between "kitten" and "sitting"?
int levenshteinDistance(const string &s, const string &t) {
int m = s.size(), n = t.size();
vector dp(m + 1, vector<int>(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++) {
if (s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1];
else dp[i][j] = 1 + min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]});
}
}
return dp[m][n];
}
cout << levenshteinDistance("kitten", "sitting") << endl;
medium
A. 4
B. 6
C. 5
D. 3
Solution
Step 1: Understand the strings and operations
"kitten" to "sitting" requires substitutions and insertions.
Step 2: Calculate minimum edits
Substitute 'k'->'s', substitute 'e'->'i', insert 'g' at end, total 3 edits.