Shows Runtime Error: IndexOutOfBound for input "bb" but it is not an error

  • 0

    I have written a JAVA code as below.
    class Solution {
    static int maxLength, lo;

    public static String longestPalindrome(String s) {
    	if(s.length() < 2) return s;
    	for(int i=0; i< s.length()-1; i++){
    		extendPalindrome(s, i, i);
    	return s.substring(lo, lo + maxLength);
    public static void extendPalindrome(String s, int j, int k){
    	while(j>=0 && k < s.length() && s.charAt(j) == s.charAt(k)){
    	if(maxLength < k - j - 1){
    		lo = j+1;
    		maxLength = k - j - 1;


    It is running successfully but when I try to submit this code, it shows me that it has a runtime error on Line 12. Below is the exact error.
    "Line 12: java.lang.StringIndexOutOfBoundsException: String index out of range: 3".

    I tried to debug it in the debugger Leetcode provides but this code is running successfully in that debugger. Even I tried to run it on my system and it works over there.

    Can anyone help me with this problem?

    FYI: I solution is based on one of the solution I saw on stackoverflow.

Log in to reply

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