Given two strings S and P, the task is to find the largest palindrome possible string by choosing characters from the given strings S and P after rearranging the characters.
Note: If many answers are possible, then find the lexicographically smallest T with maximum length.
Input: S = “abad”, T = “eeff”
From the first string “aba” and from the second string “effe” are palindromes.
Use these two to make a new palindrome T “aefbfea” which is lexicographically smallest.
Also T is of maximum length possible.
Note: “ada”, although this will give T of same length i.e., 7 but that won’t be lexicographically smallest.
Input: S = “aeabb”, T = “dfedf”
From the first string “abeba” and from the second string “dfefd”, are palindrome. Combine the two to get T: “abdeffedba” is lexicographically smallest.
Approach: The idea is to take all the characters from the strings S and P which are present even a number of times respectively. As the substrings, A and B of both the string have all characters which are present even number of times in S and P respectively to become the largest palindrome from parent string. Below are the steps:
- Take a single character from strings S and P, such that they will be present in the mid of palindromes taken from each string respectively. Place that unique element in mid of T.
- The possible cases to take a unique element from substring A and B are:
- If a unique element is present in both A and B, then take both of them because it will increase the length of T by 2.
- If unique elements are present in both S and P but not the same element, then take that which is small because that will make lexicographically the smallest palindrome T.
- If no unique element is present in both, then make the string T just with elements from A and B.
- Use the unordered map to take count of characters occurring even number of times in parent string. Also, use another map to have count of characters to be used in making T.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of String S or P.
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Length of longest common prefix possible by rearranging strings in a given array
- Count of Palindromic Strings possible by swapping of a pair of Characters
- Print all possible palindromic string formed using any pair of given strings
- Longest palindromic string possible by concatenating strings from a given array
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Largest number not greater than N which can become prime after rearranging its digits
- Count of distinct XORs formed by rearranging two Binary strings
- Count of non-palindromic strings of length M using given N characters
- Lexicographically largest string possible for a given cost of appending characters
- Lexicographically largest possible String after removal of K characters
- Check if permutation of a given string can be made palindromic by removing at most K characters
- Smallest number by rearranging digits of a given number
- Given a string, print all possible palindromic partitions
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Find all distinct palindromic sub-strings of a given string
- Find all palindromic sub-strings of a given string | Set 2
- Distinct palindromic sub-strings of the given string using Dynamic Programming
- Count pairs of non-overlapping palindromic sub-strings of the given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.