Longest substring whose characters can be rearranged to form a Palindrome | Function to get the length of longest substring whose characters can be arranged to form a palindromic string ; To keep track of the last index of each xor ; Initialize answer with 0 ; Now iterate through each character of the string ; Convert the character from [ a , z ] to [ 0 , 25 ] ; Turn the temp - th bit on if character occurs odd number of times and turn off the temp - th bit off if the character occurs ever number of times ; If a mask is present in the index Therefore a palindrome is found from index [ mask ] to i ; If x is not found then add its position in the index dict . ; Check for the palindrome of odd length ; We cancel the occurrence of a character if it occurs odd number times ; Given String ; Length of given string ; Function call
function longestSubstring ( s , n ) { var index = new Map ( ) ; var answer = 0 ; var mask = 0 ; index [ mask ] = - 1 ; for ( var i = 0 ; i < n ; i ++ ) { var temp = s [ i ] . charCodeAt ( 0 ) - 97 ; mask ^= ( 1 << temp ) ; if ( index [ mask ] ) { answer = Math . max ( answer , i - index [ mask ] ) ; } else index [ mask ] = i ; for ( var j = 0 ; j < 26 ; j ++ ) { var mask2 = mask ^ ( 1 << j ) ; if ( index [ mask2 ] ) { answer = Math . max ( answer , i - index [ mask2 ] ) ; } } } return answer ; } var s = " " ; var n = s . length ; document . write ( longestSubstring ( s , n ) ) ;