-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Levenshtein_distance.cpp
39 lines (31 loc) · 1.13 KB
/
Levenshtein_distance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>
#include <algorithm>
int levenshteinDistance(const std::string& str1, const std::string& str2) {
int m = str1.length();
int n = str2.length();
// Create a 2D matrix to store the distances
std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1, 0));
// Initialize the matrix
for (int i = 0; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
if (i == 0) {
dp[i][j] = j; // If the first string is empty
} else if (j == 0) {
dp[i][j] = i; // If the second string is empty
} else if (str1[i - 1] == str2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + std::min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]});
}
}
}
return dp[m][n];
}
int main() {
std::string str1 = "kitten";
std::string str2 = "sitting";
int distance = levenshteinDistance(str1, str2);
std::cout << "Levenshtein distance between '" << str1 << "' and '" << str2 << "' is: " << distance << std::endl;
return 0;
}