Longest palindrome formed by concatenating and reordering strings of equal length | Java program to find the Longest palindrome that can be formed by concatenating and reordering given N Strings of equal length ; Function to print the longest palindrome ; Printing every String in left vector ; Printing the palindromic String in the middle ; Printing the reverse of the right vector to make the final output palindromic ; Function to find and print the longest palindrome that can be formed ; Inserting each String in the set ; Vectors to add the Strings in the left and right side ; To add the already present palindrome String in the middle of the solution ; Iterating through all the given Strings ; If the String is a palindrome it is added in the middle ; Checking if the reverse of the String is already present in the set ;  ; Driver code
import java . util . * ; class GFG { static void printPalindrome ( Vector < String > left , String mid , Vector < String > right ) { for ( String x : left ) System . out . print ( x ) ; System . out . print ( mid ) ; Collections . reverse ( right ) ; for ( String x : right ) System . out . print ( x ) ; System . out . println ( ) ; } static void findPalindrome ( Vector < String > S , int N , int M ) { HashSet < String > dict = new HashSet < String > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { dict . add ( S . get ( i ) ) ; } Vector < String > left = new Vector < String > ( ) , right = new Vector < String > ( ) ; String mid = " " ; for ( int i = 0 ; i < N ; i ++ ) { String t = S . get ( i ) ; t = reverse ( t ) ; if ( t == S . get ( i ) ) mid = t ; else if ( dict . contains ( t ) ) { left . add ( S . get ( i ) ) ; right . add ( t ) ; dict . remove ( S . get ( i ) ) ; dict . remove ( t ) ; } } printPalindrome ( left , mid , right ) ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " tab " , " one " , " bat " } ; Vector < String > S = new Vector < String > ( Arrays . asList ( arr ) ) ; int M = 3 ; int N = S . size ( ) ; findPalindrome ( S , N , M ) ; } }