forked from HemangTheHuman/hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion-Deletion_Conversion_string.cpp
More file actions
55 lines (39 loc) · 1.26 KB
/
Insertion-Deletion_Conversion_string.cpp
File metadata and controls
55 lines (39 loc) · 1.26 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Minimum Insertions/Deletions to Convert String A to String B
// We are given two strings, str1 and str2. We are allowed the following operations:
// Delete any number of characters from string str1.
// Insert any number of characters in string str1.
// We need to tell the minimum operations required to convert str1 to str2.
#include <bits/stdc++.h>
using namespace std;
int lcs(string s1, string s2) {
int n=s1.size();
int m=s2.size();
vector<vector<int>> dp(n+1,vector<int>(m+1,-1));
for(int i=0;i<=n;i++){
dp[i][0] = 0;
}
for(int i=0;i<=m;i++){
dp[0][i] = 0;
}
for(int ind1=1;ind1<=n;ind1++){
for(int ind2=1;ind2<=m;ind2++){
if(s1[ind1-1]==s2[ind2-1])
dp[ind1][ind2] = 1 + dp[ind1-1][ind2-1];
else
dp[ind1][ind2] = 0 + max(dp[ind1-1][ind2],dp[ind1][ind2-1]);
}
}
return dp[n][m];
}
int canYouMake(string str1, string str2){
int n = str1.size();
int m = str2.size();
int k = lcs(str1,str2);
return (n-k)+(m-k);
}
int main() {
string str1= "abcd";
string str2= "anc";
cout<<"The Minimum operations required to convert str1 to str2: "<<
canYouMake(str1,str2);
}