-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkPalindrome.cpp
More file actions
51 lines (43 loc) · 1.12 KB
/
kPalindrome.cpp
File metadata and controls
51 lines (43 loc) · 1.12 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
#include <bits/stdc++.h>
using namespace std;
bool is_k_palin(string s,int k);
int main() {
// your code goes here
//A string is k palindrome if it can be transformed into a palindrome on removing at most k characters from it.
//Your task is to complete the function is_k_palin which takes two arguments a string str and a number N .
//Your function should return true if the string is k palindrome else it should return false.
int t;
cin>>t;
while(t--)
{
string s ;
cin>>s;
int k;
cin>>k;
cout<<is_k_palin(s,k)<<endl;
}
return 0;
}// } Driver Code Ends
/*You are required to complete this function*/
bool is_k_palin(string s,int k)
{
string rev = string(s.rbegin(),s.rend());
int n=s.length();
int dp[n+1][n+1];
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
if(i==0 || j==0)
dp[i][j]=0;
else if(s[i-1]==rev[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
int res=n-dp[n][n];
if(k>=res)
return true;
return false;
}