## 5. Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:

Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.


Example:

Input: "cbbd"

Output: "bb"


b'
\n\n

## Summary

\n

This article is for intermediate readers. It introduces the following ideas:\nPalindrome, Dynamic Programming and String Manipulation. Make sure you understand what a palindrome means. A palindrome is a string which reads the same in both directions. For example, is a palindome, is not.

\n

## Solution

\n
\n

#### Approach #1 (Longest Common Substring) [Accepted]

\n

Common mistake

\n

Some people will be tempted to come up with a quick solution, which is unfortunately flawed (however can be corrected easily):

\n
\n

Reverse and become . Find the longest common substring between and , which must also be the longest palindromic substring.

\n
\n

This seemed to work, let\xe2\x80\x99s see some examples below.

\n

For example, , .

\n

The longest common substring between and is , which is the answer.

\n

Let\xe2\x80\x99s try another example: , .

\n

The longest common substring between and is . Clearly, this is not a valid palindrome.

\n

Algorithm

\n

We could see that the longest common substring method fails when there exists a reversed copy of a non-palindromic substring in some other part of . To rectify this, each time we find a longest common substring candidate, we check if the substring\xe2\x80\x99s indices are the same as the reversed substring\xe2\x80\x99s original indices. If it is, then we attempt to update the longest palindrome found so far; if not, we skip this and find the next candidate.

\n

This gives us an Dynamic Programming solution which uses space (could be improved to use space). Please read more about Longest Common Substring here.

\n
\n

#### Approach #2 (Brute Force) [Time Limit Exceeded]

\n

The obvious brute force solution is to pick all possible starting and ending positions for a substring, and verify if it is a palindrome.

\n

Complexity Analysis

\n
\n
• \n

Time complexity : .\nAssume that is the length of the input string, there are a total of such substrings (excluding the trivial solution where a character itself is a palindrome). Since verifying each substring takes time, the run time complexity is .

\n
• \n
• \n

Space complexity : .

\n
• \n
\n
\n

#### Approach #3 (Dynamic Programming) [Accepted]

\n

To improve over the brute force solution, we first observe how we can avoid unnecessary re-computation while validating palindromes. Consider the case . If we already knew that is a palindrome, it is obvious that must be a palindrome since the two left and right end letters are the same.

\n

We define as following:

\n

\n\n

\n

Therefore,

\n

\n\n

\n

The base cases are:

\n

\n\n

\n

\n\n

\n

This yields a straight forward DP solution, which we first initialize the one and two letters palindromes, and work our way up finding all three letters palindromes, and so on...

\n

Complexity Analysis

\n
\n
• \n

Time complexity : .\nThis gives us a runtime complexity of .

\n
• \n
• \n

Space complexity : .\nIt uses space to store the table.

\n
• \n
\n

\n

Could you improve the above space complexity further and how?

\n
\n

#### Approach #4 (Expand Around Center) [Accepted]

\n

In fact, we could solve it in time using only constant space.

\n

We observe that a palindrome mirrors around its center. Therefore, a palindrome can be expanded from its center, and there are only such centers.

\n

You might be asking why there are but not centers? The reason is the center of a palindrome can be in between two letters. Such palindromes have even number of letters (such as ) and its center are between the two s.

\n
public String longestPalindrome(String s) {\n    int start = 0, end = 0;\n    for (int i = 0; i < s.length(); i++) {\n        int len1 = expandAroundCenter(s, i, i);\n        int len2 = expandAroundCenter(s, i, i + 1);\n        int len = Math.max(len1, len2);\n        if (len > end - start) {\n            start = i - (len - 1) / 2;\n            end = i + len / 2;\n        }\n    }\n    return s.substring(start, end + 1);\n}\n\nprivate int expandAroundCenter(String s, int left, int right) {\n    int L = left, R = right;\n    while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {\n        L--;\n        R++;\n    }\n    return R - L - 1;\n}\n
\n

Complexity Analysis

\n
\n
• \n

Time complexity : .\nSince expanding a palindrome around its center could take time, the overall complexity is .

\n
• \n
• \n

Space complexity : .

\n
• \n
\n

#### Approach #5 (Manacher\'s Algorithm) [Accepted]

\n

There is even an algorithm called Manacher\'s algorithm, explained here in detail. However, it is a non-trivial algorithm, and no one expects you to come up with this algorithm in a 45 minutes coding session. But, please go ahead and understand it, I promise it will be a lot of fun.

\n
'