forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_72.cpp
More file actions
26 lines (23 loc) · 692 Bytes
/
_72.cpp
File metadata and controls
26 lines (23 loc) · 692 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
//Problem Link : https://leetcode.com/problems/edit-distance/
//Method : DP
class Solution {
public:
int minDistance(string word1, string word2) {
int n=word1.length();
int m=word2.length();
int dp[n+1][m+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
if(i==0)
dp[i][j]=j;
else if(j==0)
dp[i][j]=i;
else if(word1[i-1]==word2[j-1])
dp[i][j]=dp[i-1][j-1];
else
dp[i][j]=1+min(dp[i-1][j],min(dp[i][j-1],dp[i-1][j-1]));
}
}
return dp[n][m];
}
};