-
Notifications
You must be signed in to change notification settings - Fork 0
/
28. Min. no. of deletion in a string to make it a palindrome
64 lines (39 loc) · 1.52 KB
/
28. Min. no. of deletion in a string to make it a palindrome
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
56
57
58
59
60
61
62
63
64
28 Minimum number of deletion in a string to make it a palindrome
Video Link : https://youtu.be/CFwCCNbRuLY
GFG Practice Link : https://practice.geeksforgeeks.org/problems/minimum-number-of-deletions4610/1
Approach :-->>
i). find the longest common pallindromic subsequence
ii). return the difference of it from lenght of string
------->>>>>>>>>>>------------>>>>>>>>>>>>________CODE________<<<<<<<<<<<<------------<<<<<<<<<<<<<<<<<<<<<<<<<<<
int minDeletions(string str, int n) {
//complete the function here
string a= str, b=str;
reverse(b.begin(),b.end());
int size= a.size(), size2= b.size();
// declaration of the the 2-D vector
vector<vector<int>> t (size+1, vector<int> (size2+1));
// filling the base case condition
for(int i=0;i<size+1;i++){
t[i][0]=0;
}
for(int j=0;j<size2+1;j++)
{
t[0][j]=0;
}
// applying LCS concept by top-down method
for(int i=1;i<size+1;i++)
{
for(int j=1;j<size2+1;j++)
{
if(a[i-1]==b[j-1])
{
t[i][j]=1+t[i-1][j-1];
}
else{
t[i][j]= max(t[i][j-1] , t[i-1][j]);
}
}
}
int count= t[size][size2];
return n-count;
}