You are given a string ‘STR’. Your task is to find the longest palindromic substring in ‘STR’. If there is more than one palindromic substring with the maximum length, return the one with the smaller start index.
A string is said to be palindrome if the reverse of the string is the same as the actual string. For example, “abba” is a palindrome, but “abbc” is not a palindrome.
For example:You are given ‘STR’ = “abacd”. Then our answer will be “aba”. “aba” is the longest palindromic substring in ‘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.
Output Format:
For each test case, print the longest palindromic substring in ‘STR’.
The output of each test case will be printed in a separate line.
1 <= T <= 10
0 <= STR.LENGTH <= 5000
‘STR’ contains only lowercase English letters.
Time limit: 1 sec
Note:
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
2
abacd
abbcd
aba
bb
For the first test case, you are given ‘STR’ = “abacd”. Then our answer will be “aba”. “aba” is the longest palindromic substring in ‘STR’.
For the second test case, you are given ‘STR’ = “abbcd”. Then our answer will be “bb”. “bb” is the longest palindromic substring in ‘STR’.
2
abba
acbbc
abba
cbbc
For each center position, try to increase the answer by one until it's possible.
Prerequisite: Expand Around The Centre.
Manacher’s algorithm optimizes the “Expand Around The Centre” solution by using some insights into how palindromes work.
Let C be the center of the longest-length palindrome we have encountered till now. And let L and R be the left and right boundaries of this palindrome, i.e., the left-most character index and the right-most character index, respectively.
Take an example: STR = “abacabacabb”
When going from left to right, when i is at index 1, the longest palindromic substring is “aba” (length = 3).
The longest palindromic substring for the given string is 9 when the palindrome is centered at index 5:
CURR_L: For any palindrome centered at a center C the CURR_L of the mirror of the given index j in the left direction, the mirror of index j is j’ such that
J’ = 2*C - J
For palindrome “abacaba”, the mirror of j is j’ and the mirror of j’ is j.
for some j, its mirror j’ for palindrome “abacaba”
We try to “expand” the palindrome at each i (CURR_R). When I say the word expand, it means that I’ll check whether there exists a palindrome centered at i and if there exists one, I’ll store the “expansion length” to the left or the right in a new array called LEN_ARR[] or P[] array. If the palindrome at i expands beyond the current right boundary r, then c is updated to i and new l, r is found and updated.
Let’s take the previously discussed palindrome “abacaba” which is centered at i = 3.
P[i] = 3 as expansion length for palindrome centered at i is 3
Hence, the LEN_ARR[] or P[] array stores the expansion length of the palindrome centered at each index. But we don’t need to manually go to each index and expand to check the expansion length every time. This is exactly where Manacher’s algorithm optimizes better than brute force by using some insights on how palindromes work. Let’s see how the optimization is done.
Let’s see the P[] array for the string “abacaba”.
When i = 4, the index is inside the scope of the current longest palindrome, i.e., i < r. So, instead of naively expanding at i, we want to know the minimum expansion length that is certainly possible at i so that we can expand on that minimum P[i] and see, instead of doing from the start. So, we check for mirror i’. As long as the palindrome at index i’ does NOT expand beyond the left boundary (l) of the current longest palindrome, we can say that the minimum certainly possible expansion length at i is P[i’].
Here we are only talking about the minimum possible expansion length, the actual expansion length could be more and, we’ll find that out by expanding later on. In this case, P[4] = P[2] = 0. We try to expand but still, P[4] remains 0.
If the palindrome at index i’ expands beyond the left boundary (l) of the current longest palindrome, we can say that the minimum possible expansion length at i is r-i.
For example: STR = “acacacb”
In the above diagram, palindrome centered at i’ expands beyond the left boundary
So, P[4] = 5–4 = 1
Keep Updating P[i] to the minimum certainly possible expansion length. Now the only thing left is to expand at i after P[i], so we check characters from the index (P[i] + 1) and keep expanding at i.
If this palindrome expands beyond r, update c to i and r to (i + P[i]). The P[] or LEN_ARR[] array is now filled, and the maximum value in this array gives us the longest palindromic substring in the given string.
Algorithm:
O(N), where N is the length of the string.
Since the manacher’s algorithm takes O(N) time, the overall time complexity will be O(N).
O(N), where N is the length of the string.
We have created a LEN_ARR[] or P[] array,. which takes O(N), so the overall space complexity will be O(N).