/*
10.最长回文子序列 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"
10.最长回文子序列 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"
*/
class Solution {
public:
string longestPalindrome(string s) {
string max;
for(int i = 0; i < s.size(); i++)
{
string str = palindrome(s, i);
if(str.size() > max.size())
{
max = str;
}
}
return max;
}
string palindrome(string s, int i)
{
if(s.size()==1){
return s;
}else{
string str;
int low1 = i;
int high1 = i;
while(low1-1>=0 && high1+1 < s.size() && s[low1-1] == s[high1+1])
{
low1--;
high1++;
};
int low2 = i-1;
int high2 = i;
while(low2>=0 && high2 < s.size() && s[low2] == s[high2])
{
low2--;
high2++;
};
low2++;
high2--;
if(high1 - low1 >= high2 - low2)
{
for(int k = low1; k <= high1; k++)
str.push_back(s[k]);
}
else
{
for(int k = low2; k <= high2; k++)
str.push_back(s[k]);
}
return str;
}
}
};