New update is available. Click here to update.

Last Updated: 18 Nov, 2020

Moderate

```
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
```

Approaches

- Generate substrings of the given string such that substring having greater length will be generated first.
- To do this, run a loop where iterator len will go from N to 1, where N is the length of the given string.
- Run a nested loop and fix an iterator j that will point at the starting index of the substring.
- Get the substring from j to j+len.
- If the substring is a palindrome, return the substring (As the substring will be of the longest length and minimum starting index).

- If the string length is less than or equal to 1 then return the string, as one length string is always palindromic.
- Initialize a DP array of data type boolean, DP[i][j] will store false if INPUT[i,j] is not palindromic otherwise it will store true.
- Store all the diagonal elements (DP[i][i]) as true, as INPUT[i,i] will always be palindromic.
- For substring of length 2, check if INPUT[i] is equal to INPUT[i+1] then store DP[i][i+1] as true.
- Run a loop for length of substring greater than 2, fill the DP array diagonally, To calculate DP[i][j], check the value of DP[i+1][j-1], if the value is true and INPUT[i] is same as INPUT[j], then we make DP[i][j] true otherwise false.
- For every DP[i][j] true, update the length and starting index of the substring.
- Return the substring of the string having starting index as start and of maximum length.

- If the string length is less than or equal to 1 then return the string, as a single character is always palindromic.
- The idea is to generate all even length and odd length palindromes and keep track of the longest palindrome seen so far.
- To generate odd length palindrome, run a loop where i will be between 0 to N-1, Fix centre i, and expand in both directions for longer palindromes, odd length palindromes will have a character at the centre.
- Similarly, for even length palindrome, fix the centre as i, i+1, and expand in both directions, even palindrome will have partition between ith char and i+1th char as the centre.
- If the length of the current palindromic substring length is greater than update the starting length of the string and length of the palindromic substring.
- Return the substring of the string having starting index as start and of maximum length.

For expanding :

- For expanding around a center i for odd length, initialize two variables left and right to i and go until LEFT and RIGHT are in range and INPUT[LEFT] == INPUT[RIGHT]. Decrement the LEFT and increment the RIGHT.
- For expanding around a centre i and i+1 for even length, initialise two variables LEFT = i and RIGHT = i+1, and go until LEFT and RIGHT are in range and INPUT[LEFT] == INPUT[RIGHT]. Decrement the LEFT and increment the RIGHT.
- Return the length of the palindromic substring.

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.