class Solution {
public:
string find_str(int l, int r, string s)
{
string ans ="";
for(int i = l; i <= r; i++)
{
ans += s[i];
}
return ans;
}
string longestPalindrome(string s) {
string ans = "";
int max = 0;
if(s.length() == 1)
return s;
for(int i = 0; i < s.length() -1; i++)
{
int left = i, right = s.length()-1;
while((left <= right))
{
if(s[left] != s[right])
{
right+= left-i-1;
left = i;
}
else
{
left++;
right--;
}
}
cout << left << " " << right << "\n";
if(left > right)
{
string str1 = find_str(i, right+left-i, s);
if(max < str1.length())
{
ans = str1;
max = str1.length();
}
}
else
{
ans = s[i];
}
}
return ans;
}
};
class Solution {
public:
int GetLength(int l, int r, string str)
{
int count = 0;
while(l >= 0 && r < str.length() && str[l] == str[r])
{
l--;
r++;
count++;
}
if(count == 0) return 0;
return r-l-1;
}
string longestPalindrome(string s) {
if(s.length() <= 1)
{
return s;
}
string ans = "";
int odd_len = 0, even_len = 0, left = 0, right = 0;
for(int i = 0; i < s.length(); i++)
{
odd_len = GetLength(i, i, s);
even_len = GetLength(i, i+1, s);
if(odd_len > right-left+1) right = i+(odd_len/2), left = i-(odd_len/2);
if(even_len > right-left+1) right = i+(even_len/2), left = i-(even_len/2)+1;
}
for(int i = left; i <= right; i++)
{
ans += s[i];
}
return ans;
}
};