Longest palindromic string possible by concatenating strings from a given array | Python3 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 is itself a palindrome or not ; Check if the reverse of the 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
def max_len ( s , N , M ) : NEW_LINE INDENT set_str = { } NEW_LINE for i in s : NEW_LINE INDENT set_str [ i ] = 1 NEW_LINE DEDENT left_ans , right_ans = [ ] , [ ] NEW_LINE mid = " " NEW_LINE for i in range ( N ) : NEW_LINE INDENT t = s [ i ] NEW_LINE t = t [ : : - 1 ] NEW_LINE if ( t == s [ i ] ) : NEW_LINE INDENT mid = t NEW_LINE DEDENT elif ( t in set_str ) : NEW_LINE INDENT left_ans . append ( s [ i ] ) NEW_LINE right_ans . append ( t ) NEW_LINE del set_str [ s [ i ] ] NEW_LINE del set_str [ t ] NEW_LINE DEDENT DEDENT for x in left_ans : NEW_LINE INDENT print ( x , end = " " ) NEW_LINE DEDENT print ( mid , end = " " ) NEW_LINE right_ans = right_ans [ : : - 1 ] NEW_LINE for x in right_ans : NEW_LINE INDENT print ( x , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE M = 3 NEW_LINE s = [ " omg " , " bbb " , " ffd " , " gmo " ] NEW_LINE max_len ( s , N , M ) NEW_LINE DEDENT