Longest Substring that can be made a palindrome by swapping of characters | Function to find the Longest substring that can be made a palindrome by swapping of characters ; Initialize dp array of size 1024 ; Initializing mask and res ; Traverse the string ; Find the mask of the current character ; Finding the length of the longest substring in s which is a palindrome for even count ; Finding the length of the longest substring in s which is a palindrome for one odd count ; Finding maximum length of substring having one odd count ; dp [ mask ] is minimum of current i and dp [ mask ] ; Return longest length of the substring which forms a palindrome with swaps ; Input String ; Function Call
function longestSubstring ( s ) { let dp = new Array ( 1024 ) . fill ( 1 ) ; let res = 0 , mask = 0 ; dp [ 0 ] = - 1 ; for ( let i = 0 ; i < s . length ; ++ i ) { mask ^= 1 << ( s [ i ] - ' ' ) ; res = Math . max ( res , i - dp [ mask ] ) ; for ( let j = 0 ; j <= 9 ; ++ j ) res = Math . max ( res , i - dp [ mask ^ ( 1 << j ) ] ) ; dp [ mask ] = Math . min ( dp [ mask ] , i ) ; } return res ; } let s = " " ; document . write ( longestSubstring ( s ) ) ;