|
| 1 | +--- |
| 2 | +id: levenshtein-distance |
| 3 | +title: Levenshtein Distance Algorithm |
| 4 | +sidebar_label: 0007 - Levenshtein Distance Algorithm |
| 5 | +tags: [Levenshtein Distance, String Matching, Algorithm, C++, Problem Solving] |
| 6 | +description: This is a solution for implementing the Levenshtein Distance Algorithm for measuring the difference between two sequences. |
| 7 | +--- |
| 8 | + |
| 9 | +## Problem Statement |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +The Levenshtein Distance algorithm calculates the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into another. It is widely used in applications like spell checking, DNA sequencing, and natural language processing. |
| 14 | + |
| 15 | +### Examples |
| 16 | + |
| 17 | +**Example 1:** |
| 18 | + |
| 19 | +```plaintext |
| 20 | +Input: |
| 21 | +String1: "kitten" |
| 22 | +String2: "sitting" |
| 23 | +Output: |
| 24 | +3 |
| 25 | +Explanation: The Levenshtein Distance between "kitten" and "sitting" is 3 (kitten -> sitten -> sittin -> sitting). |
| 26 | +``` |
| 27 | + |
| 28 | +### Constraints |
| 29 | +- The length of both strings can be up to 10^3. |
| 30 | + |
| 31 | +## Solution of Given Problem |
| 32 | + |
| 33 | +### Intuition and Approach |
| 34 | + |
| 35 | +The Levenshtein Distance algorithm uses dynamic programming to efficiently compute the edit distance between two strings. It constructs a matrix where the cell at position (i, j) contains the Levenshtein Distance between the first i characters of the first string and the first j characters of the second string. |
| 36 | + |
| 37 | +### Approaches |
| 38 | + |
| 39 | +#### Codes in Different Languages |
| 40 | + |
| 41 | +<Tabs> |
| 42 | + <TabItem value="cpp" label="C++"> |
| 43 | + <SolutionAuthor name="sjain1909"/> |
| 44 | + ```cpp |
| 45 | + #include <bits/stdc++.h> |
| 46 | + using namespace std; |
| 47 | + |
| 48 | + int levenshteinDistance(const string& str1, const string& str2) { |
| 49 | + int len1 = str1.size(); |
| 50 | + int len2 = str2.size(); |
| 51 | + vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1)); |
| 52 | + |
| 53 | + for (int i = 0; i <= len1; ++i) { |
| 54 | + for (int j = 0; j <= len2; ++j) { |
| 55 | + if (i == 0) { |
| 56 | + dp[i][j] = j; |
| 57 | + } else if (j == 0) { |
| 58 | + dp[i][j] = i; |
| 59 | + } else if (str1[i - 1] == str2[j - 1]) { |
| 60 | + dp[i][j] = dp[i - 1][j - 1]; |
| 61 | + } else { |
| 62 | + dp[i][j] = 1 + min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return dp[len1][len2]; |
| 68 | +} |
| 69 | + |
| 70 | +int main() { |
| 71 | + string str1, str2; |
| 72 | + cout << "Enter the first string: "; |
| 73 | + cin >> str1; |
| 74 | + cout << "Enter the second string: "; |
| 75 | + cin >> str2; |
| 76 | + |
| 77 | + int distance = levenshteinDistance(str1, str2); |
| 78 | + cout << "The Levenshtein Distance between \"" << str1 << "\" and \"" << str2 << "\" is " << distance << ".\n"; |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
| 82 | + ``` |
| 83 | + </TabItem> |
| 84 | +</Tabs> |
| 85 | + |
| 86 | +### Complexity Analysis |
| 87 | + |
| 88 | +- **Time Complexity:** $O(N * M)$ where N is the length of the first string and M is the length of the second string. |
| 89 | +- **Space Complexity:** $O(N * M)$ for the dynamic programming matrix. |
| 90 | + |
| 91 | +## Video Explanation of Given Problem |
| 92 | + |
| 93 | + <LiteYouTubeEmbed |
| 94 | + id="obWXjtg0L64" |
| 95 | + params="autoplay=1&autohide=1&showinfo=0&rel=0" |
| 96 | + title="Problem Explanation | Solution | Approach" |
| 97 | + poster="maxresdefault" |
| 98 | + webp |
| 99 | + /> |
| 100 | +--- |
| 101 | + |
| 102 | +<h2>Authors:</h2> |
| 103 | + |
| 104 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 105 | +{['sjain1909'].map(username => ( |
| 106 | + <Author key={username} username={username} /> |
| 107 | +))} |
| 108 | +</div> |
0 commit comments