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 is still left in the array ; Update the answer with all strings of pair1 ; Update the answer with palindromic s1 ; Update the answer with all strings of pair2 ; Driver Code
def longestPalindrome ( a , n ) : NEW_LINE INDENT pair1 = [ 0 ] * n NEW_LINE pair2 = [ 0 ] * n NEW_LINE r = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = a [ i ] NEW_LINE s = s [ : : - 1 ] NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( a [ i ] != " " and a [ j ] != " " ) : NEW_LINE INDENT if ( s == a [ j ] ) : NEW_LINE INDENT pair1 [ r ] = a [ i ] NEW_LINE pair2 [ r ] = a [ j ] NEW_LINE r += 1 NEW_LINE a [ i ] = " " NEW_LINE a [ j ] = " " NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT s1 = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = a [ i ] NEW_LINE a [ i ] = a [ i ] [ : : - 1 ] NEW_LINE if ( a [ i ] != " " ) : NEW_LINE INDENT if ( a [ i ] == s ) : NEW_LINE INDENT s1 = a [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT ans = " " NEW_LINE for i in range ( r ) : NEW_LINE INDENT ans = ans + pair1 [ i ] NEW_LINE DEDENT if ( s1 != " " ) : NEW_LINE INDENT ans = ans + s1 NEW_LINE DEDENT for j in range ( r - 1 , - 1 , - 1 ) : NEW_LINE INDENT ans = ans + pair2 [ j ] NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT a1 = [ " aba " , " aba " ] NEW_LINE n1 = len ( a1 ) NEW_LINE longestPalindrome ( a1 , n1 ) NEW_LINE a2 = [ " abc " , " dba " , " kop " , " abd " , " cba " ] NEW_LINE n2 = len ( a2 ) NEW_LINE longestPalindrome ( a2 , n2 ) NEW_LINE