Skip to content

Commit e4db819

Browse files
committed
Added Solution - GfG to GitHub
1 parent 0811977 commit e4db819

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Easy/K-Palindrome/kpalindrome.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//{ Driver Code Starts
2+
// Initial Template for C++
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
// } Driver Code Ends
8+
// User function Template for C++
9+
10+
class Solution{
11+
public:
12+
int solve(string& a,string& b,int n){
13+
vector<vector<int>> dp(n+1,vector<int>(n+1,0));
14+
for(int i=n-1;i>=0;i--){
15+
for(int j=n-1;j>=0;j--){
16+
if(a[i]==b[j]) dp[i][j]=1+dp[i+1][j+1];
17+
else dp[i][j]=max(dp[i+1][j],dp[i][j+1]);
18+
}
19+
}
20+
return dp[0][0];
21+
}
22+
int kPalindrome(string str, int n, int k)
23+
{
24+
string b=str;
25+
reverse(b.begin(),b.end());
26+
int x=solve(str,b,n);
27+
return (x+k>=n);
28+
}
29+
};
30+
31+
//{ Driver Code Starts.
32+
33+
int main(){
34+
int t;
35+
cin>>t;
36+
while(t--){
37+
int n, k;
38+
cin>>n>>k;
39+
string str;
40+
cin>>str;
41+
42+
Solution ob;
43+
cout<<ob.kPalindrome(str, n, k)<<endl;
44+
}
45+
return 0;
46+
}
47+
// } Driver Code Ends

0 commit comments

Comments
 (0)