


You are given ‘STR’ = “aabcd”. Then our answer will be “dcbaabcd”. We can form a palindrome by adding ‘d’, ‘c’, and ‘b’ in front of ‘STR’.
The first line contains an integer 'T' which denotes the number of test cases.
The first line of each test case contains a string denoting the input string.
For each test case, print the shortest palindrome that can be formed by adding characters in front of ‘STR’.
The output of each test case will be printed in a separate line.
1 <= T <= 10
1 <= STR.LENGTH <= 5000
‘STR’ contains only lowercase English letters.
Time limit: 1 sec
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
In this approach, we will find the largest palindromic substring of ‘STR’ from the beginning, and we will reverse the remaining substring of ‘STR’ and append it to the beginning of ‘STR’.
5454545
Let’s write ‘STR’ as ‘PSTR’ + ‘RSTR’, where ‘PSTR’ is the palindromic substring of ‘STR’ and ‘RSTR’ is the remaining substring of ‘STR’. Then our answer would be REVERSE(‘RSTR’) + ‘STR’.5454545
Algorithm:
In approach 1, we found the largest palindromic substring of ‘STR’ from the beginning using string comparison, which takes O(N) time. In this approach, we will look at a more efficient to find the largest palindromic substring by reducing the size of the string to search for the substring without checking the complete substring each time.
To reduce the size of the string to be searched, we use two pointers, ‘L’ and ‘R’. Initially, we will et ‘L’ as 0 and iterate ‘R’ from ‘N’ - 1 to 0. We will increment ‘L’ by 1 whenever STR[L] equals ‘STR[R]. Now, the substring of ‘STR’ from index 0 to ‘L’ must contain the largest palindromic substring.
Algorithm:
Prerequisite: KMP algorithm for string matching.
In approach 1, we saw that if we write ‘STR’ as ‘PSTR’ + ‘RSTR’, where ‘PSTR’ is the palindromic substring of ‘STR’ and ‘RSTR’ is the remaining substring of ‘STR’. Then our answer would be REVERSE(‘RSTR’) + ‘STR’.
Now define a string ‘KSTR’ =‘STR’ + # + REVERSE(‘STR’) = (‘PSTR’ + ‘RSTR’) + # + (REVERSE(‘RSTR’) + ‘PSTR’). Now, we can run KMP on ‘KSTR’ to find the longest prefix, which is also a suffix that will be ‘PSTR’.
Once we know ‘PSTR’ then we can easily find ‘RSTR’. Then our answer will be REVERSE(‘RSTR’) + ‘STR’.
For example, if ‘STR’ is “abac”, then ‘KSTR’ will be “abacd#dcaba”. Now. We can see that “aba” is the longest prefix which is also a suffix. So:
‘PSTR’ -> “aba”.
‘RSTR’ -> “cd”
REVERSE(‘RSTR’) -> “dc”.
Answer -> REVERSE(‘RSTR’) + ‘STR’ = “dc” + “aba” = “dcaba”.
To find the longest prefix, which is also a suffix, we will use KMP. We will define a table ‘TABLE’ that will store the index of the character, which will be compared with the next character if the current character is the same as the prefix’s last character. If a match is found, we will increment the length of the previous substring solution, i.e., we will set ‘TABLE[I]’(‘I’ is the index) as ‘TABLE[I - 1]’ + 1. Otherwise, we will shorten the match string length and jump to the prefix part that we used to match suffix ended at ‘I’ - 1. Then, in the end, the value of the last element of ‘TABLE,’ i.e., TABLE[TABLE.LENGTH - 1] will represent the longest common prefix's last index which we will use to find the longest common prefix.
Algorithm:
Maintain a function ‘GETTABLE(string STR)’: