0

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;
}
DarthQuack
  • 1,254
  • 3
  • 12
  • 22
  • 1
    Can you please tell what all libraries you are including – Pratap Alok Raj May 16 '21 at 05:52
  • 2
    Try using s.at(index) instead of `s[index]` and the issue becomes obvious. IMHO `j-k` takes a negative value, and you test `(j-k)>=0` and `(i+k)<=` too late. – 273K May 16 '21 at 05:52
  • One suggestion from my side, in the below while loop try to have the last condition as the first condition and the code should run fine then : - Like this -- while((i+k)<=s.length()-1 && s[i+k]==s[j-k] && (j-k)>=0) Try and let me know if that works for you – Pratap Alok Raj May 16 '21 at 05:55
  • Indenting your code properly would be the first step to a happy debug session. By the way, "longest palindromic subsequence" is not the same as "longest palindromic substring", make sure you are solving the right problem. This algorithm does neither, even after fixing the bug (test on "abaa"). – n. m. could be an AI May 16 '21 at 06:10
  • @AlokRaj included bits/stdc++.h – bazinga123 May 16 '21 at 07:58
  • @AlokRaj no even after changing the condition it doesn't work – bazinga123 May 16 '21 at 07:59
  • @S.M. could you please explain how j-k is negative didnt get that – bazinga123 May 16 '21 at 08:08
  • @n.'pronouns'm. i tested by giving the input abaa its giving the correct output which is aa .Also thanks for the suggestion i changed the heading – bazinga123 May 16 '21 at 08:09
  • The correct output is aba, not aa. – n. m. could be an AI May 16 '21 at 08:14
  • @n.'pronouns'm. oh yes I'll check where im going wrong – bazinga123 May 16 '21 at 08:15
  • Please give a [mcve] – JHBonarius May 16 '21 at 09:03
  • @bazinga123 bits/stdc++.h has issues as it's an unstandardized hack naming a GCC internal header directly instead of individual standard headers like , and . Please include the individual libraries instead of using this bits/stdc++.h header. Below post contains some good info on this :- https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H. Try it once – Pratap Alok Raj May 16 '21 at 12:15
  • Your code is incomplete; in particular, it seems to be missing a `main()` function and at least one `#include`. And it's not minimal, either - there's too much that's incidental to your problem. Please [edit] your code so it's a [mcve] of your problem (including any necessary inputs, but preferably not needing any), then we can try to reproduce and solve it. You should also read [ask]. – Toby Speight May 16 '21 at 16:30

0 Answers0