-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_distance_iter.cpp
More file actions
47 lines (44 loc) · 1.55 KB
/
edit_distance_iter.cpp
File metadata and controls
47 lines (44 loc) · 1.55 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
// SPACE 2*M : actually you can take minimum of both string's length(swap) and then, SPACE= O(N+M)
int minDistMinMemory(string word1, string word2) {
int n = word1.size();
int m = word2.size();
vector<vector<int>> curr(2, vector<int>(m + 1));
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= m; j++)
{
if (i == 0 || j == 0)
curr[i & 1][j] = i + j;
else if (word1[i - 1] == word2[j - 1])
curr[i & 1][j] = curr[i & 1 ^ 1][j - 1];
else curr[i & 1][j] = 1 + min({
curr[i & 1][j - 1], // inserting word2[j-1] after i-1 in word1
curr[i & 1 ^ 1][j], // deleting word1[i-1]
curr[i & 1 ^ 1][j - 1] // replacing word1[i-1] by word2[j-1]
});
}
}
return curr[n & 1][m];
}
// SPACE : N*M
int minDistanceMoreMemory(string word1, string word2) {
int n = word1.size();
int m = word2.size();
int dp[n + 1][m + 1]; // 1 indexed
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= m; j++)
{
if (i == 0 || j == 0)
dp[i][j] = i + j; // if i=0 then j deletions and vice versa
else if (word1[i - 1] == word2[j - 1])
dp[i][j] = dp[i - 1][j - 1];
else dp[i][j] = 1 + min({
dp[i][j - 1], // inserting word2[j-1] after i-1 in word1
dp[i - 1][j], // deleting word1[i-1]
dp[i - 1][j - 1] // replacing word1[i-1] by word2[j-1]
});
}
}
return dp[n][m];
}