Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Solution {
public:
string longestPalindrome(string s) {
if(s == "")
return s;
int len = s.size();
int maxsLen = 0;
bool arr[len][len];
string finalString;
for(int i = 1; i <= len; i++)
{
for(int start = 0; start < len; start++)
{
int end = start + i - 1;
if(end >= len)
break;
arr[start][end] = (i == 1 || i == 2 || arr[start + 1][end - 1])&&(s[start] == s[end]);
if(arr[start][end] && i > maxsLen)
{
finalString = s.substr(start, i);
maxsLen = i;
}
}
}
return finalString;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment