Time Limit Exceeded Last executed input


  • 0
    X

    public class Solution {

    public static void main(String[] args) {
    	String s = "a";
    	System.out.println(longestPalindrome(s));
    }
    
    public static String longestPalindrome(String s){
    	int lens = 0;
    	int max = 0;
    	String lp = null;
    	int len = s.length();
    	for(int i=0;i<len;i++){
    		for(int j=len-1;j>=0 && (j-i)>=max;j--){
    			if(s.charAt(i)==s.charAt(j)){
    				String str = s.substring(i,j+1);
    				if(isPalindrome(str)){
    					lens = j-i;
    					if(max<=lens){
    						max = lens;
    						lp = str;
    					}
    					break;
    				}
    			}
    		}
    	}
    	return lp;
    }
    
    public static boolean isPalindrome(String str){
    	int len = str.length();
    	int i = 0;
    	for(i=1;i<len/2-1;i++){
    		if(str.charAt(i)!=str.charAt(len-i-1))
    			break;
    	}
    	return (i>=len/2-1);
    }
    

    }


Log in to reply
 

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