Java 2D DP Solution clean code


  • 1
       
    public class Solution {
        public boolean isMatch(String s, String p) {
            int sLen = s.length();
            int pLen = p.length();
            boolean[][] dp = new boolean[sLen+1][pLen+1];
            dp[0][0]=true;
            for(int j=0;j<pLen;j++){
                if(p.charAt(j)=='*')dp[0][j+1]=dp[0][j];
            }
            for(int i=0;i<sLen;i++){
                for(int j=0;j<pLen;j++){
                    if(s.charAt(i)==p.charAt(j) || p.charAt(j)=='?')dp[i+1][j+1]=dp[i][j];
                    else if(p.charAt(j)=='*')dp[i+1][j+1]=dp[i+1][j] || dp[i][j+1];
                }
            }
            return dp[sLen][pLen];
        }
    }
       
    

Log in to reply
 

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