📖 Scenario: Imagine you are building a spell checker that suggests corrections for misspelled words. To do this, you need to measure how different two words are by counting the minimum number of changes needed to turn one word into the other. This is called the Levenshtein edit distance.
🎯 Goal: Build a program that calculates the Levenshtein edit distance between two given words using dynamic programming.
📋 What You'll Learn
Create two string variables with exact values
Create a 2D integer array to store distances
Use nested loops to fill the distance matrix
Print the final edit distance value
💡 Why This Matters
🌍 Real World
Spell checkers, auto-correct features, DNA sequence analysis, and natural language processing use edit distance to find similarities between strings.
💼 Career
Understanding dynamic programming and string algorithms like Levenshtein distance is important for software engineers working on text processing, search engines, and AI applications.
Progress0 / 4 steps
1
Create the input strings
Create two string variables called word1 and word2 with the exact values "kitten" and "sitting" respectively.
DSA C#
Hint
Use string type and assign the exact words as values.
2
Initialize the distance matrix
Create a 2D integer array called distance with size (word1.Length + 1, word2.Length + 1). Initialize the first row with values from 0 to word2.Length and the first column with values from 0 to word1.Length using for loops.
DSA C#
Hint
Use two for loops to fill the first row and first column with incremental values.
3
Fill the distance matrix with core logic
Use nested for loops with variables i from 1 to word1.Length and j from 1 to word2.Length. Inside, check if word1[i - 1] equals word2[j - 1]. If equal, set cost to 0, else 1. Then set distance[i, j] to the minimum of three values: distance[i - 1, j] + 1, distance[i, j - 1] + 1, and distance[i - 1, j - 1] + cost.
DSA C#
Hint
Use nested loops and the formula for Levenshtein distance to fill the matrix.
4
Print the final edit distance
Print the value of distance[word1.Length, word2.Length] which is the Levenshtein edit distance between word1 and word2.
DSA C#
Hint
The final edit distance is stored at distance[word1.Length, word2.Length]. Use Console.WriteLine to print it.
Practice
(1/5)
1. What does the Levenshtein Edit Distance between two strings measure?
easy
A. The total length of both strings combined
B. The maximum length of common substring between two strings
C. The number of characters that are the same in both strings
D. The minimum number of single-character edits to change one string into another
Solution
Step 1: Understand the definition of Levenshtein distance
It counts the minimum edits (insertions, deletions, substitutions) to convert one string into another.
Step 2: Compare with other options
Options B and C describe different string metrics, not edit distance.
Final Answer:
The minimum number of single-character edits to change one string into another -> Option D
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
Assuming it measures string length
2. Which of the following C# method signatures correctly represents a function to compute Levenshtein distance between two strings s1 and s2?
easy
A. private string LevenshteinDistance(string s1, string s2)
B. public void LevenshteinDistance(int s1, int s2)
C. public int LevenshteinDistance(string s1, string s2)
D. public int LevenshteinDistance(int[] s1, int[] s2)
Solution
Step 1: Check parameter types for string inputs
The function should accept two strings, so parameters must be string types.
Step 2: Check return type for distance value
Distance is an integer, so return type should be int, not void or string.
Final Answer:
public int LevenshteinDistance(string s1, string s2) -> Option C
Quick Check:
Function signature matches string inputs and int output [OK]
Hint: Function takes strings, returns int distance [OK]
Common Mistakes:
Using void return type instead of int
Using int parameters instead of strings
Returning string instead of int
3. What is the output of the following C# code snippet?
var s1 = "kitten"; var s2 = "sitting"; Console.WriteLine(LevenshteinDistance(s1, s2));
int LevenshteinDistance(string a, string b) { int[,] dp = new int[a.Length + 1, b.Length + 1]; for (int i = 0; i <= a.Length; i++) dp[i, 0] = i; for (int j = 0; j <= b.Length; j++) dp[0, j] = j; for (int i = 1; i <= a.Length; i++) { for (int j = 1; j <= b.Length; j++) { int cost = (a[i - 1] == b[j - 1]) ? 0 : 1; dp[i, j] = Math.Min(Math.Min(dp[i - 1, j] + 1, dp[i, j - 1] + 1), dp[i - 1, j - 1] + cost); } } return dp[a.Length, b.Length]; }
medium
A. 3
B. 4
C. 5
D. 6
Solution
Step 1: Understand the strings and operations
"kitten" to "sitting" requires substitutions, insertions, deletions.
Step 2: Calculate Levenshtein distance manually
Changes: k->s (substitution), e->i (substitution), t->t (no change), t->t (no change), e->i (substitution), n->n (no change), add g (insertion) = 3 substitutions + 1 insertion = 4 edits total.
4. Identify the bug in this Levenshtein distance C# code snippet:
int LevenshteinDistance(string a, string b) { int[,] dp = new int[a.Length, b.Length]; for (int i = 0; i < a.Length; i++) dp[i, 0] = i; for (int j = 0; j < b.Length; j++) dp[0, j] = j; for (int i = 1; i < a.Length; i++) { for (int j = 1; j < b.Length; j++) { int cost = (a[i] == b[j]) ? 0 : 1; dp[i, j] = Math.Min(Math.Min(dp[i - 1, j] + 1, dp[i, j - 1] + 1), dp[i - 1, j - 1] + cost); } } return dp[a.Length - 1, b.Length - 1]; }
medium
A. Array size should be (a.Length+1, b.Length+1) and loops adjusted
B. Cost calculation should use a[i-1] and b[j-1]
C. Return statement should be dp[a.Length, b.Length]
D. No bug, code is correct
Solution
Step 1: Check array size and indexing
dp array must be sized (a.Length+1, b.Length+1) to include empty prefixes; current size misses last row/column.
Step 2: Adjust loops to run from 0 to length inclusive
Loops should run to <= length to fill dp correctly; current loops miss last indices.
Final Answer:
Array size should be (a.Length+1, b.Length+1) and loops adjusted -> Option A
Quick Check:
dp array size must include empty string row/column [OK]
Hint: dp array must be one larger than string lengths [OK]
Common Mistakes:
Using array size equal to string length only
Incorrect loop bounds causing index errors
Ignoring empty string base cases
5. Given two strings s1 = "intention" and s2 = "execution", what is the Levenshtein distance between them? Use the standard operations: insert, delete, substitute.
hard
A. 5
B. 6
C. 4
D. 7
Solution
Step 1: Analyze differences between "intention" and "execution"
Compare characters and count minimal edits: substitutions and insertions/deletions.
Step 2: Calculate minimal edits manually
Edits: i->e (substitution), n->x (substitution), t->c (substitution), insert u, substitute n->o, substitute n->n (no change), total 5 edits.
Final Answer:
5 -> Option A
Quick Check:
Distance("intention", "execution") = 5 [OK]
Hint: Count substitutions and insertions carefully [OK]
Common Mistakes:
Underestimating substitutions needed
Ignoring insertions or deletions
Confusing with Hamming distance
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.