- #1

member 428835

I'm trying to find the max length of a sub-string palindrome. My logic is to have a left-most index called start that starts from string loc 0 and traverses to the end of the string via a for loop. Next I was thinking a while loop as long as L < R: I'd have a left index L=start and a right index R = string.length(). If ##L \neq R##, then ##R = R-1## and continue the while loop. However, if ##L = R## then ##L++## and ##R--##, we count this as a match, and continue. Only until L and R are the same (odd palindrome) or 1 away (even palindrome) do we replace the max palindrome length. However, I have a few bugs. Here's what I have, and any help is much appreciated.

C++:

```
#include <iostream>
#include <vector>
#include <string> // for string class
class Solution {
public:
int longestPalindrome(std::string str) {
int n = str.length();
int maxLen = 1;
int count = 0;
for (int start = 0; start < n; start++) {
int L = start;
int R = n - 1;
while (R > L) {
if (str[R] != str[L]) {
R--;
continue;
}
count ++;
if (L == R) {
maxLen = std::max(maxLen, 2*count+1);
count = 0;
break;
}
if (L == R - 1) {
maxLen = std::max(maxLen, 2*count);
count = 0;
break;
}
L++;
R--;
}
}
return maxLen;
}
};
int main()
{
std::string s_ = "yyureeeerl";
Solution ob1;
auto sol = ob1.longestPalindrome(s_);
std::cout << sol << "\n";
return 0;
}
```