File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Minimum number of deletions. - GFG Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ // { Driver Code Starts
2+ #include < bits/stdc++.h>
3+ using namespace std ;
4+ int minDeletions (string str, int n);
5+
6+ int main (){
7+ int t;
8+ cin >> t;
9+ while (t--){
10+
11+ int n;
12+ cin >> n;
13+ string s;
14+ cin >> s;
15+ cout << minDeletions (s, n) << endl;
16+
17+ }
18+ return 0 ;
19+ }
20+ // } Driver Code Ends
21+
22+ int LCS (string a, string b, int n)
23+ {
24+ int t[n + 1 ][n + 1 ];
25+ for (int i = 0 ; i <= n; i++)
26+ {
27+ for (int j = 0 ; j <= n; j++)
28+ {
29+ if (i == 0 || j == 0 )
30+ t[i][j] = 0 ;
31+ else if (a[i - 1 ] == b[j - 1 ])
32+ t[i][j] = 1 + t[i - 1 ][j - 1 ];
33+ else
34+ t[i][j] = max (t[i - 1 ][j], t[i][j - 1 ]);
35+ }
36+ }
37+
38+ return t[n][n];
39+ }
40+
41+ int LPS (string s)
42+ {
43+ string a = s;
44+ reverse (s.begin (), s.end ());
45+ string b = s;
46+ int n = s.size ();
47+ return LCS (a, b, n);
48+ }
49+
50+ int minDeletions (string s, int n) {
51+ return s.size () - LPS (s);
52+ }
You can’t perform that action at this time.
0 commit comments