0% found this document useful (0 votes)
275 views3 pages

Levenshtein Distance Implementation Guide

The document outlines tasks for implementing the Levenshtein Distance algorithm, including a basic edit distance function and a cost-constrained version. It provides specific test cases for both implementations and suggests applying the algorithm to create a spell-checker. Additionally, it discusses optimization strategies for improving time and space complexity in handling large strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
275 views3 pages

Levenshtein Distance Implementation Guide

The document outlines tasks for implementing the Levenshtein Distance algorithm, including a basic edit distance function and a cost-constrained version. It provides specific test cases for both implementations and suggests applying the algorithm to create a spell-checker. Additionally, it discusses optimization strategies for improving time and space complexity in handling large strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Levenshtein Distance

Task 1: Implement the Levenshtein Algorithm

Implement the Algorithm


You are given two strings, s1 and s2. Your task is to implement a function EditDistance(s1, s2) that computes the
minimum cost to transform s1 into s2. The minimum cost is the smallest total cost of a sequence of operations
(insertions, deletions, and substitutions) required to convert s1 into s2, where each operation has a cost of 1.
Function Signature:
int EditDistance (string s1, string s2);
Constraints:
• m, n ≤ 1000 (The lengths of s1 and s2 are at most 1000 characters).
Your code should be written in the programming language of your choice.
Upload Snapshots of Output After Testing the Code
Apply your function to the following test cases:
• Input: "kitten", "sitting"
Expected Output: 3
• Input: "flaw", "lawn"
Expected Output: 2
• Input: "algorithm", "logarithm"
Expected Output: 3
Please upload snapshots of the expected output for each of the above test cases.
Once the output snapshots are confirmed, submit the code for further review.
Task 2: Minimum Weighted Edit Distance Calculation

Implement the Cost-Constrained Algorithm


You are given two strings, s1 and s2, and the respective costs for three types of operations: insertion, deletion,
and substitution. Your task is to compute the minimum weighted edit distance between these two strings.
The minimum weighted edit distance is the smallest total cost required to transform s1 into s2, considering the
following operation costs:
• Insertion: The cost of inserting a character into s1 to match a character in s2 is denoted as Ci.
• Deletion: The cost of deleting a character from s1 to match s2 is denoted as Cd.
• Substitution: The cost of replacing a character in s1 with one from s2 is denoted as Cs.
Function Signature:
int EditDistance(string s1, string s2, int Ci, int Cd, int Cs);
Constraints:
• m, n ≤ 1000 (The lengths of s1 and s2 are at most 1000 characters).

© OneBanc Technologies Pvt. Ltd. Private & Confidential


• 1 ≤ Ci, Cd, Cs ≤ 1000 (Cost values for operations).
Your code should be written in the programming language of your choice.
Test Cases
Apply your function to the following test cases:
• Input: "kitten", "sitting", Ci=1, Cd=2, Cs=3
Expected Output: 7
• Input: "flaw", "lawn", Ci=2, Cd=2, Cs=1
Expected Output: 4
• Input: "algorithm", "logarithm", Ci=1, Cd=3, Cs=2
Expected Output: 6
Please upload snapshots of the expected output for each of the above test cases.
Once the output snapshots are confirmed, submit the code for further review.
Task 3: Apply It to a Real Problem

Let's say you are required to build a spell-checker for a text editor. Using the levenshteinDistance function, write
a program that suggests the closest word from a dictionary for a given input.
Example:
• Dictionary: ["cred", "bet", "shat", "that", "brad", "cart", "brat", "card"]
• Ci=1, Cd=1, Cs=1
• Input: "dat"
• Expected Output: "bet", "shat", "that", "cart", "brat"
Please submit the code in the programming language of your choice.
Task 4: Optimization Task (Bonus)

The Levenshtein algorithm typically uses a dynamic programming (DP) table of size n×m, where n and m are the
lengths of the two strings. This requires:
• Time complexity: O(n×m) for filling up the DP table.
• Space complexity: O(n×m) for storing the table.
For very large strings, this approach may:
• Use too much memory to store the DP table.
• Take too long to compute the result.
Please explain how you would optimize the function further.

© OneBanc Technologies Pvt. Ltd 2 Private & Confidential


© OneBanc Technologies Pvt. Ltd 3 Private & Confidential

You might also like