题解 | #通配符匹配#
通配符匹配
http://www.nowcoder.com/practice/e96f1a44d4e44d9ab6289ee080099322
public boolean isMatch(String s, String p) {
if( p == null || p.length() == 0){
return s == null || s.length() == 0;
}
int sLen = s.length();
int pLen = p.length();
//dp[i][j] s的 0... i-1 元素 能够被 p的 0....j-1 元素匹配
boolean[][] dp = new boolean[sLen + 1][pLen + 1];
dp[0][0] = true;
for(int j = 1;j<=pLen;j++){
dp[0][j] = p.charAt(j-1) == '*' && dp[0][j-1];
}
for(int i = 1;i<= sLen;i++){
for(int j = 1;j <=pLen;j ++){
char a = s.charAt(i-1);
char b = p.charAt(j-1);
if(a == b || b == '?'){
dp[i][j] = dp[i-1][j-1];
}else if(b == '*'){
dp[i][j] = dp[i-1][j] || dp[i][j-1];
}
}
}
return dp[sLen][pLen];
}
}