From 213e8548a004ba3957e822f59ea3f17f5236cd5d Mon Sep 17 00:00:00 2001 From: PraneshASP Date: Fri, 25 Sep 2020 17:02:43 +0530 Subject: [PATCH] added-edit_distance-solution --- cpp/_72.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 cpp/_72.cpp diff --git a/cpp/_72.cpp b/cpp/_72.cpp new file mode 100644 index 0000000000..18f4f8e4d7 --- /dev/null +++ b/cpp/_72.cpp @@ -0,0 +1,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]; + } +}; \ No newline at end of file