You are given a string ‘str’. You need to find out whether the string is a K-Palindrome or not.
A string is called a K-Palindrome if, after removing at most ‘k’ characters from the string, it can be Transformed into a Palindrome.
For Example :str = “ababba”, k = 3.
In this example, If we remove one ‘b’ from the 3rd position, then the final string will be “ababa” which is a palindrome.
Hence the answer will be True.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘k’, denoting the maximum number of characters you can remove from the string.
The second line of the test case contains the string “str”.
Output Format :
For each test case, print whether the following string is a K-Palindrome or not.
Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= |S| <= 1000
1 <= k <= 30
Time limit: 1 sec
2
1
abba
2
abcca
True
True
In the first test case, We can make the string “abba” a palindrome by removing ‘b’ at position 2 or 3.
Hence the answer is True.
In the second test case, We can make the string palindrome by removing either 1 ‘b’ or two ‘c’.
Hence the answer is True.
2
1
abc
1
abb
False
True
Make a copy of string “str”, reverse it, and check how many characters do not match the original string.
In this approach, we will make a copy of the original string and reverse it and recursively check if the current characters are equal or not.
The steps are as follows:
O( 2 ^ |S| ), where |S| is the length of the String.
As we are Checking for every possible palindrome.
Hence the time complexity is O( 2 ^ N ).
O( 1 ).
No extra space is used.
Hence the space complexity is O(1).