Missing test case?


  • 1
    E

    My solution is below:

        public class Solution {
        public String longestPalindrome(String s) 
       {
    		 if(s.length() == 1) 
    		 {
    			return s;
    		 }
    	     char[] ch = s.toCharArray();
    	     int max = 0;
    	     int start = 0;
    	 
    	     for (int i = 1; i < ch.length; i++)
    	     {
    			for (int j = Math.max(i-2,0); j < i; j++) 
    			{
    				if (ch[i]==ch[j])
    				{
    						int m = i, n = j;
    						while(m < ch.length && n >= 0)
    						{
    							if (ch[m] == ch[n]) 
    							{
    								m++;
    								n--;
    							}
    							else break;
    						}
    						m--;
    						n++;
    						int count = m - n + 1;
    						if (count > max) 
    						{
    							max = count;
    							start = n;
    						}
    				}	
    			}
    		}
    	    StringBuilder sbBuilder = new StringBuilder();
    	    for (int i = 0; i < max; i++) 
    	    {
    		    sbBuilder.append(ch[start+i]);
    		}
    	    return sbBuilder.toString();
    	}
    
    }
    

    when the test case is "abcda", the expected answer is "a", but my answer is "". However, my solution has been accepted, so there is a missing test case and my solution is wrong?


  • 0

    Thanks, I have added your test case.


Log in to reply
 

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