A substring is a contiguous segment of a string.
The first line of input contains a single integer 'T', representing the number of test cases or queries to be run.
Then the 'T' test cases follow.
The first and only one of each test case contains a string 'STR'.
For every test case, print the longest palindromic substring.
If there are multiple possible answers then you need to print the substring which has the lowest starting index.
Do not print anything. It has already been taken care of. Just implement the given function.
Try to solve it using O(1) space complexity.
1 <= T <= 5
0 <= N <= 100
Time Limit: 1 sec
For expanding :
Manacher’s algorithm optimizes our 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 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 to 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 are 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 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.
Now, if the palindrome at index i’ expands beyond the left boundary (l) of the current longest palindrome, we can say that the minimum certainly 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 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.
DECODE STRING
Cakes
1-3 Palindrome
Randomly Sorted
Search Pattern (KMP - Algorithm)
Search Pattern (KMP - Algorithm)
Search Pattern (KMP - Algorithm)