Longest palindromic string possible by concatenating strings from a given array | Javascript program for the above approach ; Stores the distinct Strings from the given array ; Insert the Strings into set ; Stores the left and right subStrings of the given String ; Stores the middle subString ; Traverse the array of Strings ; Reverse the current String ; Checking if the String is itself a palindrome or not ; Check if the reverse of the String is present or not ; Append to the left subString ; Append to the right subString ; Erase both the Strings from the set ; Print the left subString ; Print the middle subString ; Print the right subString ; Driver Code ; Function Call
function reverse ( input ) { let a = input . split ( " " ) ; a . reverse ( ) ; return a . join ( " " ) ; } function max_len ( s , N , M ) { let set_str = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) { set_str . add ( s [ i ] ) ; } let left_ans = [ ] ; let right_ans = [ ] ; let mid = " " ; for ( let i = 0 ; i < N ; i ++ ) { let t = s [ i ] ; t = reverse ( t ) ; if ( t == s [ i ] ) { mid = t ; } else if ( set_str . has ( t ) ) { left_ans . push ( s [ i ] ) ; right_ans . push ( t ) ; set_str . delete ( s [ i ] ) ; set_str . delete ( t ) ; } } for ( let x = 0 ; x < left_ans . length ; x ++ ) { document . write ( left_ans [ x ] ) ; } document . write ( mid ) ; ( right_ans ) . reverse ( ) ; for ( let x = 0 ; x < right_ans . length ; x ++ ) { document . write ( right_ans [ x ] ) ; } } let N = 4 , M = 3 ; let s = [ " " , " " , " " , " " ] ; max_len ( s , N , M ) ;