K-Palindrome
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.
Input Format :
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.
Constraints :
1 <= T <= 10
1 <= |S| <= 1000
1 <= k <= 30
Time limit: 1 sec
CodingNinjas
author
2y
I had done before the coding test so I solved in same way by splitting whole staring in three parts (start+mid+end).
CodingNinjas
author
2y
Recursion Approach
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:
- In the “main” ...read more
CodingNinjas
author
2y
Top-Down Dynamic Programming
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, by doing so, we are checking t...read more
CodingNinjas
author
2y
Optimised Dynamic Programming
As the value of k is very small, we can take advantage of that fact and reduce its time complexity. The approach is simple. We can only delete a maximum of ‘k’ characters ...read more
Add answer anonymously...
Top Uber Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Top HR questions asked in Uber Software Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app