Can someone explain the mistake in this code? Leetcode 44 Wildcard Matching

Viewed 18

Can Someone explain what is wrong in this code ?

It is failing on testcase s = "aa" and p = "*". I have followed recursion and dynamic programming code here

Leetcode 44 Wildcard Matching

class Solution {
    public boolean isMatch(String s, String p) {
        int n = s.length();
        int m = p.length();
        int[][] dp = new int[n][m];
        for(int[] it : dp)
            Arrays.fill(it, -1);
        return solve(n-1 , m-1, s ,p , dp);
    }
    
    public boolean solve(int i, int j, String s, String p, int[][] dp){
        if(i < 0 && j < 0) return true;
        if(i < 0 && j >=0){
            while(j>=0){
                if(p.charAt(j) + "" == "*") j--;
                else return false;
            }
            return true;
        }
        if(j < 0 && i >=0) return false;
        if(dp[i][j] != -1){
            if(dp[i][j]==1) return true;
            return false;
        }
        if(s.charAt(i) == p.charAt(j) || p.charAt(j) + "" == "?"){
            boolean temp = solve(i-1,j-1,s,p,dp);
            if(temp == false) dp[i][j] = 0;            
            else
                dp[i][j] = 1;
            return temp;
        }  
        if(p.charAt(j) + "" == "*"){
            boolean temp = solve(i-1,j,s,p,dp) || solve(i,j-1,s,p,dp);
            if(temp == false)
                dp[i][j] = 0;
            else
                dp[i][j] = 1;
            return temp;
        }
        dp[i][j] = 0;
        return false;
    }
}
0 Answers
Related