Error
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at: __n (which is 18446744073709551615) >= this->size() (which is 5)
Edit: After changing s.at instead of s[]
Getting this error in Leetcode's compiler, but my code works fine in any online compiler.
Here's my code:
string longestPalindrome(string s)
{
int i, j, flag = 0;
string max_so_far = "", max_till_now = "";
if (s.empty())
return "";
if (s.length() == 1)
return s;
for (i = 0; i < s.length(); i++) {
max_till_now = "";
for (j = s.length() - 1; j > i; j--) {
int k = 0;
if (s[i] == s[j]) {
while (s[i + k] == s[j - k] && (j - k) >= 0 && (i + k) <= s.length() - 1) {
max_till_now += s[i + k];
k++;
}
}
if (!max_till_now.empty())
break;
else
continue;
}
if (max_so_far.length() < max_till_now.length()) {
max_so_far = max_till_now;
}
}
return max_so_far;
}