This interesting problem has been featured in the famous Greplin programming challenge, and is asked quite often in the interviews. Why? Because this problem can be attacked in so many ways. There are five different solutions that I am aware of. Are you up to the challenge?
Hint:
First, make sure you understand what a palindrome means. A palindrome is a string which reads the same in both directions. For example, “aba” is a palindome, “abc” is not.
A common mistake:
Some people will be tempted to come up with a quick solution, which is unfortunately flawed (however can be corrected easily):
Reverse S and become S’. Find the longest common substring between S and S’, which must also be the longest palindromic substring.
This seemed to work, let’s see some examples below.
For example,
S = “caba”, S’ = “abac”.
The longest common substring between S and S’ is “aba”, which is the answer.
Let’s try another example:
S = “abacdfgdcaba”, S’ = “abacdgfdcaba”.
The longest common substring between S and S’ is “abacd”. Clearly, this is not a valid palindrome.
a reversed copy of a non-palindromic substring in some other part of S. To rectify this, each time we find a longest common substring candidate, we check if the substring’s indices are the same as the reversed substring’s 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.
This gives us a O(N2) DP solution which uses O(N2) space (could be improved to use O(N) space). Please read more about Longest Common Substring here.
Brute force solution, O(N3):
The obvious brute force solution is to pick all possible starting and ending positions for a substring, and verify if it is a palindrome. There are a total of C(N, 2) such substrings (excluding the trivial solution where a character itself is a palindrome).
Since verifying each substring takes O(N) time, the run time complexity is O(N3).
Dynamic programming solution, O(N2) time and O(N2) space:
To improve over the brute force solution from a DP approach, first think how we can avoid unnecessary re-computation in validating palindromes. Consider the case “ababa”. If we already knew that “bab” is a palindrome, it is obvious that “ababa” must be a palindrome since the two left and right end letters are the same.
Stated more formally below:
iff the substring S i … S j is a palindrome, otherwise false.
Therefore,
and S i = S j )
The base cases are:
P[ i, i ] ← true
P[ i, i+1 ] ← ( S
i = S
i+1 )
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…
This gives us a run time complexity of O(N2) and uses O(N2) space to store the table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | string longestPalindromeDP ( string s ) { int n = s . length ( ) ; int longestBegin = 0 ; int maxLen = 1 ; bool table [ 1000 ] [ 1000 ] = { false } ; for ( int i = 0 ; i < n ; i ++ ) { table [ i ] [ i ] = true ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) { table [ i ] [ i + 1 ] = true ; longestBegin = i ; maxLen = 2 ; } } for ( int len = 3 ; len <= n ; len ++ ) { for ( int i = 0 ; i < n - len + 1 ; i ++ ) { int j = i + len - 1 ; if ( s [ i ] == s [ j ] && table [ i + 1 ] [ j - 1 ] ) { table [ i ] [ j ] = true ; longestBegin = i ; maxLen = len ; } } } return s . substr ( longestBegin , maxLen ) ; } |
Additional exercise:
Could you improve the above space complexity further and how?
A simpler approach, O(N2) time and O(1) space:
In fact, we could solve it in O(N2) time without any extra space.
We observe that a palindrome mirrors around its center. Therefore, a palindrome can be expanded from its center, and there are only 2N-1 such centers.
You might be asking why there are 2N-1 but not N centers? The reason is the center of a palindrome can be in between two letters. Such palindromes have even number of letters (such as “abba”) and its center are between the two ‘b’s.
Since expanding a palindrome around its center could take O(N) time, the overall complexity is O(N2).
tring expandAroundCenter ( string s , int c1 , int c2 ) {
int l = c1 , r = c2 ;
int n = s . length ( ) ;
while ( l >= 0 && r <= n - 1 && s [ l ] == s [ r ] ) {
l -- ;
r ++ ;
}
return s . substr ( l + 1 , r - l - 1 ) ;
}
string longestPalindromeSimple ( string s ) {
int n = s . length ( ) ;
if ( n == 0 ) return "" ;
string longest = s . substr ( 0 , 1 ) ; // a single char itself is a palindrome
for ( int i = 0 ; i < n - 1 ; i ++ ) {
string p1 = expandAroundCenter ( s , i , i ) ;
if ( p1 . length ( ) > longest . length ( ) )
longest = p1 ;
string p2 = expandAroundCenter ( s , i , i + 1 ) ;
if ( p2 . length ( ) > longest . length ( ) )
longest = p2 ;
}
return longest ;
}
Further Thoughts:
Does an O(N) solution exist? You bet! However, it is not trivial and requires some very clever observation. The O(N) solution is explained in my next post.