Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
class Solution {
public:
string longestPalindrome(string s)
{
int n = s.size();
if(n == 0 || n == 1)
return s;
vector< vector<bool> > ok(n, vector<bool>(n, false));
string longest = "";
int maxLen = 0;
for(int i=n-1; i>=0; i--)
{
for(int j=i; j<n; j++)
{
int len = j - i + 1;
if(s[i] == s[j] && (i+1>=j-1 || ok[i+1][j-1]))
{
ok[i][j] = true;
if(len > maxLen)
{
maxLen = len;
longest = s.substr(i, len);
}
}
}
}
return longest;
}
};