-
Notifications
You must be signed in to change notification settings - Fork 0
/
Edit Distance.cpp
41 lines (31 loc) · 916 Bytes
/
Edit 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
// Memoization
class Solution {
public:
int help(string& s1,string& s2,int i,int j,vector<vector<int>>& memo)
{
// base case
if(i<0 and j<0)
return 0;
if(i<0)
return (j+1);
if(j<0)
return (i+1);
// memo check
if(memo[i][j]!=-1)
return memo[i][j];
// recursive calls
// and small calculation
if(s1[i]==s2[j])
return help(s1,s2,i-1,j-1,memo);
int a=1+help(s1,s2,i-1,j,memo);
int b=1+help(s1,s2,i-1,j-1,memo);
int c=1+help(s1,s2,i,j-1,memo);
return memo[i][j]=min({a,b,c});
}
int editDistance(string str1, string str2) {
int n=str1.length();
int m=str2.length();
vector<vector<int>> memo(n,vector<int>(m,-1));
return help(str1,str2,n-1,m-1,memo);
}
};