Maximum even length sub | C ++ code to find the maximum length of sub - string ( of even length ) which can be arranged into a Palindrome ; function that returns true if the given sub - string can be arranged into a Palindrome ; This function returns the maximum length of the sub - string ( of even length ) which can be arranged into a Palindrome ; If we reach end of the string ; if string is of even length ; if string can be arranged into a palindrome ; Even length sub - string ; Check if current sub - string can be arranged into a palindrome ; Odd length sub - string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unordered_map < int , int > countt ; bool canBePalindrome ( unordered_map < int , int > & countt ) { for ( auto key : countt ) { if ( key . second & 1 ) return false ; } return true ; } int maxPal ( string str , unordered_map < int , int > & countt , int start , int end ) { if ( end == str . length ( ) ) { if ( ( end - start ) % 2 == 0 ) if ( canBePalindrome ( countt ) ) return end - start ; return 0 ; } else { if ( ( end - start ) % 2 == 0 ) { if ( canBePalindrome ( countt ) ) { countt [ str [ end ] ] ++ ; return max ( end - start , maxPal ( str , countt , start , end + 1 ) ) ; } else { countt [ str [ end ] ] ++ ; return maxPal ( str , countt , start , end + 1 ) ; } } else { countt [ str [ end ] ] ++ ; unordered_map < int , int > c ( countt . begin ( ) , countt . end ( ) ) ; int length = maxPal ( str , c , start , end + 1 ) ; countt [ str [ end ] ] -- ; countt [ str [ start ] ] -- ; return max ( length , maxPal ( str , countt , start + 1 , end ) ) ; } } } int main ( int argc , char const * argv [ ] ) { string str = "124565463" ; int start = 0 , end = 0 ; cout << maxPal ( str , countt , start , end ) << endl ; return 0 ; }