📖 Scenario: You are building a simple text comparison tool that helps find how different two words are. This is useful in spell checkers or search suggestions.
🎯 Goal: Build a program that calculates the Levenshtein edit distance between two words. This distance tells how many single-letter changes (insertions, deletions, or substitutions) are needed to change one word into the other.
📋 What You'll Learn
Create two string variables with exact values
Create a 2D array to store distances
Use nested loops to fill the distance table using Levenshtein logic
Print the final edit distance number
💡 Why This Matters
🌍 Real World
Spell checkers, search engines, and text correction tools use edit distance to suggest correct words or find similar strings.
💼 Career
Understanding dynamic programming and string algorithms is important for software engineers working on text processing, search, and natural language processing.
Progress0 / 4 steps
1
Create the two words to compare
Create two string variables called word1 and word2 with the exact values "kitten" and "sitting" respectively.
DSA Typescript
Hint
Use const to create variables and assign the exact strings.
2
Create the 2D array to store distances
Create a 2D array called dp with dimensions word1.length + 1 by word2.length + 1. Initialize it with zeros. Then fill the first row with numbers from 0 to word2.length and the first column with numbers from 0 to word1.length.
DSA Typescript
Hint
Use Array.fill and map to create the 2D array. Use loops to fill first row and column.
3
Fill the distance table using Levenshtein logic
Use nested for loops with variables i from 1 to word1.length and j from 1 to word2.length. Inside, if word1[i - 1] equals word2[j - 1], set dp[i][j] to dp[i - 1][j - 1]. Otherwise, set dp[i][j] to 1 plus the minimum of dp[i - 1][j], dp[i][j - 1], and dp[i - 1][j - 1].
DSA Typescript
Hint
Use nested loops and compare characters at i - 1 and j - 1. Use Math.min to find the smallest cost.
4
Print the final edit distance
Print the value of dp[word1.length][word2.length] which is the Levenshtein edit distance between word1 and word2.
DSA Typescript
Hint
Print the bottom-right value of the dp table to get the edit distance.
Practice
(1/5)
1. What does the Levenshtein edit distance between two strings measure?
easy
A. The maximum length of common substring between two strings
B. The number of characters that are the same in both strings
C. The minimum number of single-character edits to change one string into the other
D. The total length of both strings combined
Solution
Step 1: Understand the definition of Levenshtein distance
It counts the minimum edits (insertions, deletions, substitutions) needed to convert one string into another.
Step 2: Compare with other options
Options A, C, and D describe different string metrics, not edit distance.
Final Answer:
The minimum number of single-character edits to change one string into the other -> Option C
Quick Check:
Edit distance = minimum edits [OK]
Hint: Edit distance counts minimal edits to match strings [OK]
Common Mistakes:
Confusing edit distance with longest common substring
Thinking it counts only substitutions, not insertions/deletions
Assuming it measures similarity score instead of edit count
2. Which of the following TypeScript function signatures correctly represents a function to compute Levenshtein distance between two strings s1 and s2?
easy
A. function levenshtein(s1: string, s2: string): number
B. function levenshtein(s1: number, s2: number): string
C. function levenshtein(s1: string[], s2: string[]): boolean
D. function levenshtein(s1: any, s2: any): void
Solution
Step 1: Identify correct parameter and return types
Levenshtein distance takes two strings and returns a number representing the edit distance.
Step 2: Check each option's signature
function levenshtein(s1: string, s2: string): number matches the expected input and output types. Others mismatch types or return void.
Final Answer:
function levenshtein(s1: string, s2: string): number -> Option A
Quick Check:
Input strings, output number [OK]
Hint: Input strings, output number for edit distance [OK]
Common Mistakes:
Using wrong parameter types like arrays or numbers
Returning wrong types like boolean or void
Using any type without specifying string inputs
3. What is the output of the following TypeScript code snippet?
function levenshtein(s1: string, s2: string): number {
const dp: number[][] = [];
for (let i = 0; i <= s1.length; i++) {
dp[i] = [i];
}
for (let j = 0; j <= s2.length; j++) {
dp[0][j] = j;
}
for (let i = 1; i <= s1.length; i++) {
for (let j = 1; j <= s2.length; j++) {
if (s1[i - 1] === s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1;
}
}
}
return dp[s1.length][s2.length];
}
console.log(levenshtein('kitten', 'sitting'))
medium
A. 3
B. 4
C. 5
D. 6
Solution
Step 1: Initialize DP table for 'kitten' and 'sitting'
DP table rows = 7, columns = 8. Base cases set for empty prefixes.
Step 2: Fill DP table comparing characters and calculating minimum edits
Calculations show minimum edits needed to convert 'kitten' to 'sitting' is 3 (substitute 'k'->'s', 'e'->'i', insert 'g').
Final Answer:
3 -> Option A
Quick Check:
Levenshtein('kitten', 'sitting') = 3 [OK]
Hint: Trace DP table for small strings to find edit count [OK]
Common Mistakes:
Counting only substitutions, ignoring insertions/deletions
Off-by-one errors in indexing strings
Misreading the DP initialization steps
4. Identify the bug in this TypeScript implementation of Levenshtein distance:
function levenshtein(s1: string, s2: string): number {
const dp: number[][] = [];
for (let i = 0; i < s1.length; i++) {
dp[i] = [i];
}
for (let j = 0; j < s2.length; j++) {
dp[0][j] = j;
}
for (let i = 1; i <= s1.length; i++) {
for (let j = 1; j <= s2.length; j++) {
if (s1[i - 1] === s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1;
}
}
}
return dp[s1.length][s2.length];
}
medium
A. The function should return dp[0][0] instead of dp[s1.length][s2.length]
B. The dp array should be initialized with zeros instead of indices
C. The comparison should be s1[i] === s2[j], not s1[i - 1] === s2[j - 1]
D. The loops initializing dp should run up to <= length, not < length
Solution
Step 1: Check dp initialization loops
Loops run from 0 to less than length, but dp needs size length+1 to include empty prefix cases.
Step 2: Correct loop ranges to include dp[s1.length] and dp[s2.length]
Change loops to run up to <= length to properly initialize dp edges.
Final Answer:
The loops initializing dp should run up to <= length, not < length -> Option D
Quick Check:
DP size must cover full string lengths [OK]
Hint: Initialize DP arrays with length+1 for empty string cases [OK]
Common Mistakes:
Using < length instead of <= length in loops
Incorrect string index comparisons causing off-by-one errors
Returning wrong dp cell ignoring full string lengths
5. Given two strings s1 = 'intention' and s2 = 'execution', what is the minimum edit distance using Levenshtein algorithm? Also, explain why this distance is correct.
hard
A. 6
B. 5
C. 4
D. 7
Solution
Step 1: Analyze transformations from 'intention' to 'execution'
Operations include substituting 'i'->'e', 'n'->'x', deleting 't', substituting 'n'->'c', substituting 't'->'u'. Counting these gives 5 edits.
Step 2: Confirm minimal edits with DP approach
DP table confirms minimum edits needed is 5, as fewer edits cannot convert all differing characters.
Final Answer:
5 -> Option B
Quick Check:
Minimum edits = 5 for 'intention' to 'execution' [OK]