-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdit_Distance.cpp
More file actions
33 lines (26 loc) · 869 Bytes
/
Edit_Distance.cpp
File metadata and controls
33 lines (26 loc) · 869 Bytes
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
/*
Given two strings s and t. Find the minimum number of operations that need to be performed on str1 to convert it to str2. The possible operations are:
Insert
Remove
Replace
We use DP here to solve this
*/
int editDistance(string s, string t)
{
// Code here
int m = s.length(), n = t.length();
int dp[m+1][n+1];
for(int i = 0; i<=m ; i++){
for(int j = 0; j<=n; j++){
if(i == 0)
dp[i][j] = j;
else if(j == 0)
dp[i][j] = i;
else if(s[i-1] == t[j-1]) //If there is a match , take previous digonal elemnts
dp[i][j] = dp[i-1][j-1];
else
dp[i][j] = 1+min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1]); //Chcek all the operation ,which give min
}
}
return dp[m][n];
}