From 2e83649b2cc01bbafe80c4e4396d7b1cfa65b031 Mon Sep 17 00:00:00 2001 From: anshita2907 <89690825+anshita2907@users.noreply.github.com> Date: Mon, 11 Oct 2021 10:09:31 +0530 Subject: [PATCH] Create Edit distance --- Dynamic Programming/Edit distance | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Dynamic Programming/Edit distance diff --git a/Dynamic Programming/Edit distance b/Dynamic Programming/Edit distance new file mode 100644 index 0000000..8d9a68b --- /dev/null +++ b/Dynamic Programming/Edit distance @@ -0,0 +1,35 @@ +class Solution { +public: + int minDistance(string w, string s) { + int n=w.length();int m=s.length(); + + int dp[n+1][m+1]; + dp[0][0]=0; + 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(w[i-1]!=s[j-1]) + { + dp[i][j]=1+min(dp[i-1][j-1],min(dp[i-1][j], dp[i][j-1])); + } + else + { + dp[i][j]=dp[i-1][j-1]; + } + } + } + + return dp[n][m]; + + + } +};