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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Length of the longest substring that do not contain any palindrome
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Minimum length of substring whose rotation generates a palindromic substring
- Nth Even length Palindrome
- Sum of first K even-length Palindrome numbers
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Minimum removal to make palindrome permutation
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Maximum length palindrome that can be created with characters in range L and R
- Maximum length palindromic substring such that it starts and ends with given char
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Palindrome Substring Queries
- Queries to check if substring[L...R] is palindrome or not
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Longest substring whose characters can be rearranged to form a Palindrome
- Check if a number with even number of digits is palindrome or not
- Check if a given string is Even-Odd Palindrome or not
- Maximum length L such that the sum of all subarrays of length L is less than K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Improved By : sanjeev2552