I need your help


  • 0
    S

    When I submit my code, there is a Runtime Exception, test data is "bb", suggesting that the array is out of bounds, but I test this set of data on my computer can get the correct answer. I need your help, thanks.

    class Solution {
            static int longestSize = 0, startPos = 0;
            public String longestPalindrome(String s) {
                if (s == null || s.length() == 1) {
                    return s;
                }
                for (int i = 0; i < s.length(); i++) {
                    checkOddPalindrome(s, i);
                    checkEvenPalindrome(s, i);
                }
                return s.substring(startPos, startPos + longestSize);
            }
    
            private void checkOddPalindrome(String s, int index) {
                // odd palindrome
                int start = index, end = index;
                while (start > 0 && end < s.length() - 1 && s.charAt(start - 1) == s.charAt(end + 1)) {
                    start--;
                    end++;
                }
                // start -> end
                if (end - start + 1 > longestSize) {
                    longestSize = end - start + 1;
                    startPos = start;
                }
            }
    
            private void checkEvenPalindrome(String s, int index) {
                int start = index, end = Math.min(s.length() - 1, index + 1);
                while (start > 0 && end < s.length() - 1 && s.charAt(start - 1) == s.charAt(end + 1) && s.charAt(start) == s.charAt(end)) {
                    start--;
                    end++;
                }
    
                if (end - start + 1 > longestSize && s.charAt(start) == s.charAt(end)) {
                    longestSize = end - start + 1;
                    startPos = start;
                }
            }
        }
    

Log in to reply
 

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