Given a string , the task is to find the maximum length of the sub-string of that can be arranged into a Palindrome (i.e at least one of its permutation is a Palindrome). Note that the sub-string must be of even length.
Input: str = “124565463”
“456546” is the valid sub-string
Input: str = “122313”
Approach: Use two variables: start (inclusive) and end (exclusive) to keep track of the starting and ending index of the current sub-string that is being considered in the given string. Also use a dictionary named count which keeps the record of how many times, a character occurs in the current sub-string. Now, there are two possible cases for a sub-string:
- If the length of the sub-string is odd, then it cannot be considered in the final solution.
- If the length of the sub-string is even, then it can be a possible solution only if each character in that sub-string occurs even number of times which can be done using the dictionary count. We check if each character occurs even number of times or not. If yes, then we include it as one of the possible solutions. Then we form the next sub-string by including the next character in the string which can be done by simply incrementing end and check recursively if a sub-string with greater length can be formed which satisfies the given conditions and return the maximum of all possible solutions.
Below is the implementation of the above approach:
- Length of the longest substring that do not contain any palindrome
- Maximum length substring having all same characters after k changes
- Maximum length substring with highest frequency in a string
- Maximum length palindromic substring such that it starts and ends with given char
- Maximum length palindrome that can be created with characters in range L and R
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Palindrome Substring Queries
- Minimum removal to make palindrome permutation
- Queries to check if substring[L...R] is palindrome or not
- Queries on substring palindrome formation
- Nth Even length Palindrome
- Length of the longest valid substring
- Find max length odd parity substring
- Length of the longest substring with equal 1s and 0s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.