From 9ae1d5e65db643bdf162e812822ac98c43a3e1a1 Mon Sep 17 00:00:00 2001 From: Anmol55555 <61626454+Anmol55555@users.noreply.github.com> Date: Sat, 2 Oct 2021 03:35:22 +0530 Subject: [PATCH] Added Codes to DP --- ...mum number of deletions and insertions.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Program's_Contributed_By_Contributors/Dynamic Programming/Minimum number of deletions and insertions.cpp diff --git a/Program's_Contributed_By_Contributors/Dynamic Programming/Minimum number of deletions and insertions.cpp b/Program's_Contributed_By_Contributors/Dynamic Programming/Minimum number of deletions and insertions.cpp new file mode 100644 index 0000000000..3e4797112c --- /dev/null +++ b/Program's_Contributed_By_Contributors/Dynamic Programming/Minimum number of deletions and insertions.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int minOperations(string s1, string s2) + { + int n = s1.size(), m = s2.size(); + int t[n+1][m+1]; + for(int i=0; i<=n; i++) + { + for(int j=0; j<=m; j++) + { + if(i == 0 || j == 0) + t[i][j] = 0; + } + } + + for(int i=1; i<=n; i++) + { + for(int j=1; j<=m; j++) + { + if(s1[i-1] == s2[j-1]) + t[i][j] = 1 + t[i-1][j-1]; + else + t[i][j] = max(t[i-1][j], t[i][j-1]); + } + } + + return (n - t[n][m] + m - t[n][m]); + + } + +int main() +{ + string s1, s2; + cin>>s1>>s2; + + cout<