Submission Rejected because Input is Not what its supposed to be


  • 0
    S

    Hi, I'm a pretty new member and am having trouble making a submission for the 'Longest Pallindromic Substring problem'. When I try submitting it, I will get an incorrect solution "bab" instead of "bb" for the input "cbbd". When I try inputting "cbbd" in the debugger, however, my program returns the correct answer. I can only guess that the compiler is using some saved version of the input for the first test ("babad") and applying it to this particular test (8 / 94 test). Has anyone experienced this problem?

    Just in case it's relevant, I've included my code below:

    class Solution {
        public static String high="";
        public String longestPalindrome(String s) {
            String temp="";
            int begin, next, suc;
            int diff=-1;
            int k;
            
            for (int i=0;i<s.length();i++){
                if (i<s.length()-2 && (s.charAt(i)==s.charAt(i+1) || s.charAt(i)==s.charAt(i+2))){
                    next = s.charAt(i+1);
                    if (s.charAt(i)==next){    
                        if (s.charAt(i)==s.charAt(i+2)){
                            temp+=s.charAt(i);
                            if (i+2==s.length()-1){//last char
                                temp = temp+s.charAt(i+1)+s.charAt(i+2);
                                if (temp.length()>high.length()){high=temp;}
                            }
                        }
                        else if (temp!=""){
                            
                            temp+=s.charAt(i);
                            temp+=s.charAt(i+1);
                            i=checkPd(diff+1,i+1,s);
                            if (temp.length()>high.length()){high=temp;}
                            temp="";
                        }
                        else {
                            k = checkPd(i,i+1, s);
                        }
                    }
                    else if (s.charAt(i)==s.charAt(i+2)){
                        k=checkPd(i,i+2,s)-1;
                    }  
                }
                else {
                    if (i>=s.length()-2){break;}
                    diff=i;
                }
            }
            return high;
        }
        
        public int checkPd(int l,int r, String s){
                while((l-1>=0 && r+1<s.length()) && s.charAt(l-1)==s.charAt(r+1)){
                    l--;
                    r++;
                }
            if (l>=0 && r-l>high.length()){high = s.substring(l,r+1);}
            return r;
        }
    }
    

Log in to reply
 

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