Longest palindromic String formed using concatenation of given strings in any order | Function to find the longest palindromic from given array of strings ; Loop to find the pair of strings which are reverse of each other ; Loop to find if any palindromic string is still left in the array ; Update the answer with all strings of pair1 ; Update the answer with palindromic string s1 ; Update the answer with all strings of pair2 ; Driver Code
function longestPalindrome ( a , n ) { var pair1 = Array ( n ) ; var pair2 = Array ( n ) ; var r = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var s = a [ i ] ; s = s . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; for ( var j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] != " " && a [ j ] != " " ) { if ( s == a [ j ] ) { pair1 [ r ] = a [ i ] ; pair2 [ r ++ ] = a [ j ] ; a [ i ] = " " ; a [ j ] = " " ; break ; } } } } var s1 = " " ; for ( var i = 0 ; i < n ; i ++ ) { var s = a [ i ] ; a [ i ] = a [ i ] . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; if ( a [ i ] != " " ) { if ( a [ i ] == s ) { s1 = a [ i ] ; break ; } } } var ans = " " ; for ( var i = 0 ; i < r ; i ++ ) { ans = ans + pair1 [ i ] ; } if ( s1 != " " ) { ans = ans + s1 ; } for ( var j = r - 1 ; j >= 0 ; j -- ) { ans = ans + pair2 [ j ] ; } document . write ( ans + " " ) ; } var a1 = [ " " , " " ] ; var n1 = a1 . length ; longestPalindrome ( a1 , n1 ) ; var a2 = [ " " , " " , " " , " " , " " ] ; var n2 = a2 . length ; longestPalindrome ( a2 , n2 ) ;