Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Solution {
public:
string longestPalindrome(string s) {
int start = 0, end = 0;
int len = 0;
for(int i = 0; i< s.size(); i++)
{
int len1 = PalindromeLength(s, i, i);
int len2 = PalindromeLength(s, i, i+1);
len = max(len1, len2);
if(len > end - start)
{
start = i - (len - 1)/2;
end = i + len/2;
}
}
return s.substr(start, end - start + 1);
}
int PalindromeLength(string &s, int Left, int Right)
{
int L = Left, R = Right;
while(L>=0 && R< s.size() && s[L] == s[R])
{
L--;
R++;
}
return R - L - 1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment