Largest palindromic string possible from given strings by rearranging the characters | Java program for the above approach ; Function to find largest palindrome possible from S and P after rearranging characters to make palindromic string T ; Using unordered_map to store frequency of elements mapT will have freq of elements of T ; Size of both the strings ; Take all character in mapT which occur even number of times in respective strings & simultaneously update number of characters left in map ; Check if a unique character is present in both string S and P ; Making string T in two halves half1 - first half half2 - second half ; Reverse the half2 to attach with half1 to make palindrome T ; If same unique element is present in both S and P , then taking that only which is already taken through mapT ; If same unique element is not present in S and P , then take characters that make string T lexicographically smallest ; If no unique element is present in both string S and P ; Driver code ; Given two strings S and P ; Function call
import java . util . * ; import java . lang . * ; class GFG { static String mergePalindromes ( StringBuilder S , StringBuilder P ) { Map < Character , Integer > mapS = new HashMap < > ( ) , mapP = new HashMap < > ( ) , mapT = new HashMap < > ( ) ; int n = S . length ( ) , m = P . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mapS . put ( S . charAt ( i ) , mapS . getOrDefault ( S . charAt ( i ) , 0 ) + 1 ) ; } for ( int i = 0 ; i < m ; i ++ ) { mapP . put ( P . charAt ( i ) , mapP . getOrDefault ( P . charAt ( i ) , 0 ) + 1 ) ; } for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( mapS . getOrDefault ( i , 0 ) % 2 == 0 ) { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + mapS . getOrDefault ( i , 0 ) ) ; mapS . put ( i , 0 ) ; } else { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + mapS . getOrDefault ( i , 0 ) - 1 ) ; mapS . put ( i , 1 ) ; } if ( mapP . getOrDefault ( i , 0 ) % 2 == 0 ) { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + mapP . getOrDefault ( i , 0 ) ) ; mapP . put ( i , 0 ) ; } else { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + mapP . getOrDefault ( i , 0 ) - 1 ) ; mapP . put ( i , 1 ) ; } } int check = 0 ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( mapS . getOrDefault ( i , 0 ) > 0 && mapP . getOrDefault ( i , 0 ) > 0 ) { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + 2 ) ; check = 1 ; break ; } } StringBuilder half1 = new StringBuilder ( ) , half2 = new StringBuilder ( ) ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { for ( int j = 0 ; ( 2 * j ) < mapT . getOrDefault ( i , 0 ) ; j ++ ) { half1 . append ( i ) ; half2 . append ( i ) ; } } StringBuilder tmp = half2 . reverse ( ) ; if ( check == 1 ) { return half1 . append ( tmp ) . toString ( ) ; } for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( mapS . getOrDefault ( i , 0 ) > 0 || mapP . getOrDefault ( i , 0 ) > 0 ) { half1 . append ( i ) ; return half1 . append ( tmp ) . toString ( ) ; } } return half1 . append ( tmp ) . toString ( ) ; } public static void main ( String [ ] args ) { StringBuilder S = new StringBuilder ( " aeabb " ) ; StringBuilder P = new StringBuilder ( " dfedf " ) ; System . out . println ( mergePalindromes ( S , P ) ) ; } }