Got wrong answer but output and expected are exactly the same

  • 0


  • 0

    Please read the FAQ, especially the question:

    Why is my output wrong answer even though it looks identical to expected?

    Right after your char[] declaration:

    char [] out = new char[2 * L + 1];

    I added this line of code:

    for (int i = 0; i < 2*L+1; i++)
        out[i] = 'a';

    And your code output becomes: ["11a","69a","88a","96a"].

    I guess that your code has one extra character, and since Java initialize it to \0 by default, there is actually a non-printable character at the end, even though you don't see it.

    PS: Unlike C/C++, Java does not need a null-terminated character for strings.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.