How to improve Time Limit Exceed?


  • -3
    Y

    Just start the leetcode, the following code give the error for Time exceed, how can I know my code will be no TLE, Or what will be crertria for TLE?

    class Solution {
    
    	 public  boolean isInterleave(String s1, String s2, String s3) {
    		 if(s1 == null ||s2 == null || s3 == null)
    			 return false;
    		 
    		 int l1 = s1.length();
    		 int l2 = s2.length();
    		 int l3 = s3.length();
    		 
    		 if (l3 != l1 + l2)
    			 return false;
    		 
    		 
    		 if ((s3.equals(s1)) || (s3.equals(s2))) {
    			 return true;
    		 }
    		 
    	
    		 if((l1 > 0) && (l3 > 0) &&(s3.charAt(0) == s1.charAt(0))) {
    			 if (isInterleave(s1.substring(1), s2, s3.substring(1)))
    			 	return true;
    				 
    		 }
    			
    		 if((l2 > 0) && (l3 > 0) && (s3.charAt(0) == s2.charAt(0))) {
    			 if (isInterleave(s1, s2.substring(1), s3.substring(1)))
    			 	return true;
    		 }
    		 
    		 return false;
    	 }
    }

Log in to reply
 

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