## Find if String is K-Palindrome or not in JavaGiven a string S, determine whether it is a K-palindrome. When at most K characters are removed from a K-palindrome string, the string becomes a palindrome. Here, the task is to remove at most K characters from the given string in order to convert it into its reverse. In basic terms, the issue is an Edit Distance variant. We can change the parameters of the Edit Distance problem so that the input consists of the provided text and its reverse and that the only action permitted is deletion. Since the provided string is being compared to its reverse, we shall equalize them by removing a maximum of N characters from the first string and N characters from the second. Since 2*N <= 2*K is supposed to be true, then the string is said to be k-palindrome.
String str = "abccbba" int k = 1
Yes, the string is a K-palindrome.
For the given string "abccbba," as the k value is 1, only 1 element has to be removed. Hence, if str[4] = b is removed, then the remaining string is "abccba," which is a palindrome. Hence, the string is a K-Palindrome.
String str = "abcdeca" int k = 2
Yes, the string is a K-palindrome.
For the given string "abcdeca," as the k value is 2, so 2 elements have to be removed. Hence, if str[1] = b and str[4] = e or str[3] = d is removed, then the remaining string is "acdca" or "aceca," which is a palindrome. Hence, the string is a K-Palindrome.
String str = "badac" int k = 1
No, the string is not a K-palindrome.
For the given string "badac", as the k value is 1 so, 1 has an element that has to be removed, but even though it cannot be in a palindromic structure, hence the string is not a K-Palindrome. ## Approach: Naïve Recursive Method## Algorithm:Start processing from the left or right side of both strings and go through each character one by one. There are two possibilities for each set of characters we cross over, so let's start from the right corner.
## Implementation:
Yes, it is a K-Palindrome
The Time complexity is O(2n), and It increases exponentially. In the worst scenario, we might have to perform O(2n) operations, and in that scenario, the string would contain all distinct characters. The Space Complexity is O(1). ## Approach: Dynamic ProgrammingIn order to determine if a given string is a K-Palindrome or not, this method utilizes the use of a dynamic programming algorithm. It builds a table that stores the subproblem findings and iterates through the strings, comparing characters to determine the minimum number of operations are needed to change one string into another. Three scenarios are taken into consideration by the method, which applies the appropriate steps in each case: when one string is empty when the strings' final characters match, and when the strings' final characters disagree. In order to determine if the string is a K-Palindrome or not, it finally determines whether the least number of operations needed to turn it into a palindrome is equal to or less than k*2. ## Algorithm:
## Implementation:
Yes, it is a K-Palindrome
The Time complexity is O(n2), where n represents the length of the given string. The Space Complexity is also O(n2), which is used for creating a 2D dp array. |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]