Maximum even length sub | Python3 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
from collections import defaultdict NEW_LINE def canBePalindrome ( count ) : NEW_LINE INDENT for key in count : NEW_LINE INDENT if count [ key ] % 2 != 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def maxPal ( string , count , start , end ) : NEW_LINE INDENT if end == len ( string ) : NEW_LINE INDENT if ( end - start ) % 2 == 0 : NEW_LINE INDENT if canBePalindrome ( count ) == True : NEW_LINE INDENT return end - start NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT if ( end - start ) % 2 == 0 : NEW_LINE INDENT if canBePalindrome ( count ) == True : NEW_LINE INDENT count [ string [ end ] ] += 1 NEW_LINE return max ( end - start , maxPal ( string , count , start , end + 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT count [ string [ end ] ] += 1 NEW_LINE return maxPal ( string , count , start , end + 1 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT count [ string [ end ] ] += 1 NEW_LINE length = maxPal ( string , count . copy ( ) , start , end + 1 ) NEW_LINE count [ string [ end ] ] -= 1 NEW_LINE count [ string [ start ] ] -= 1 NEW_LINE return max ( length , maxPal ( string , count , start + 1 , end ) ) NEW_LINE DEDENT DEDENT DEDENT string = '124565463' NEW_LINE start , end = 0 , 0 NEW_LINE count = defaultdict ( lambda : 0 ) NEW_LINE print ( maxPal ( string , count , start , end ) ) NEW_LINE