class Solution {
public:
string longestPalindrome(string s) {
int size = s.length();
string res("");
if (size == 0 || size == 1) {
return s;
}
for (int i = 0; i< size - 1; i++) {
//奇數
string odd = getMaxPalindrome(s, i, i);
if (odd.length() > res.length()) {
res = odd;
}
//偶數
string even = getMaxPalindrome(s, i, i + 1);
if (even.length() > res.length()) {
res = even;
}
}
return res;
}
private:
string getMaxPalindrome(string s, int left, int right) {
while (left < s.size() &&
right >= 0 &&
s[left] == s[right]) {
right++;
left--;
}
//right減回去, left加回去
//string取sub string的用法
return s.substr(left+1, (right - 1) - (left + 1) + 1);
}
};