Skip to content

Commit a035c59

Browse files
authored
added-edit_distance-solution (#100)
1 parent c6000a9 commit a035c59

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cpp/_72.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
//Problem Link : https://leetcode.com/problems/edit-distance/
3+
//Method : DP
4+
5+
class Solution {
6+
public:
7+
int minDistance(string word1, string word2) {
8+
int n=word1.length();
9+
int m=word2.length();
10+
int dp[n+1][m+1];
11+
for(int i=0;i<=n;i++){
12+
for(int j=0;j<=m;j++){
13+
if(i==0)
14+
dp[i][j]=j;
15+
else if(j==0)
16+
dp[i][j]=i;
17+
else if(word1[i-1]==word2[j-1])
18+
dp[i][j]=dp[i-1][j-1];
19+
else
20+
dp[i][j]=1+min(dp[i-1][j],min(dp[i][j-1],dp[i-1][j-1]));
21+
22+
}
23+
}
24+
return dp[n][m];
25+
}
26+
};

0 commit comments

Comments
 (0)