Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created September 14, 2018 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/7537e95c89db86281b221b6271f00af8 to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/7537e95c89db86281b221b6271f00af8 to your computer and use it in GitHub Desktop.
class Solution {
public:
bool isPalindrome(string &s)
{
int len = s.size();
for(int i = 0; i< len/2; i++)
{
if(s[i] != s[len - i - 1])
return false;
}
return true;
}
string longestPalindrome(string s) {
int len = s.size();
int iStart = 0, Max = 0;
string temp;
for(int i =0; i< len; i++)
{
for (int j = i; j < len ; j++)
{
temp = s.substr(i, j-i+1);
if(isPalindrome(temp) && j-i + 1 > Max)
{
Max = j-i+1;
iStart = i;
}
}
}
return s.substr(iStart, Max);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment