Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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