-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditdistance.cpp
More file actions
47 lines (38 loc) · 1.23 KB
/
editdistance.cpp
File metadata and controls
47 lines (38 loc) · 1.23 KB
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
40
41
42
43
44
45
46
47
#include <string>
#include <vector>
#include <algorithm>
// Calculate Levenshtein edit distance between two words
int calculateEditDistance(const std::string& word1, const std::string& word2) {
int m = word1.length();
int n = word2.length();
// Swap to ensure word2 is shorter for space optimization
if (m < n) {
return calculateEditDistance(word2, word1);
}
if (n == 0) return m;
// Use two rows instead of full matrix to save space
std::vector<int> prev(n + 1);
std::vector<int> curr(n + 1);
// Initialize first row
for (int j = 0; j <= n; ++j) {
prev[j] = j;
}
// Fill the table row by row
for (int i = 1; i <= m; ++i) {
curr[0] = i;
for (int j = 1; j <= n; ++j) {
if (word1[i - 1] == word2[j - 1]) {
curr[j] = prev[j - 1];
} else {
// Take minimum of: delete, insert, or substitute
curr[j] = 1 + std::min({
prev[j], // delete
curr[j - 1], // insert
prev[j - 1] // substitute
});
}
}
std::swap(prev, curr);
}
return prev[n];
}