Time limit exceed... Can anyone look at my code?


  • 0

    public class Solution {
    public boolean isMatch(String s, String p) {
    if(s==null||p==null) return false;
    int m=s.length(), n=p.length();
    boolean[][] dp=new boolean[m+1][n+1];
    dp[0][0]=true;
    //if s is empty, check p
    for(int i=0;i<p.length(); i++){
    if(p.charAt(i)=='*'){
    dp[0][i+1]=dp[0][i];
    }
    }

     for(int i=0; i<m; i++){
         for(int j=0; j<n; j++){
             
             if(p.charAt(j)=='?') 
                dp[i+1][j+1]=dp[i][j];
             
             else if(p.charAt(j)=='*'){
                 //* matches any sequence of character
                 //empty, sequence of character, ?*->
                 dp[i+1][j+1]=dp[i][j+1]||dp[i][j]||dp[i+1][j];
             }else if(p.charAt(j)==s.charAt(i)){
                 dp[i+1][j+1]=dp[i][j];
             }
             System.out.println(i+" "+j);
             
         }
     }
     return dp[m][n];
    }
    

    }


Log in to reply
 

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