-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path44.java
30 lines (27 loc) · 855 Bytes
/
44.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public boolean isMatch(String s, String p) {
int m = s.length();
int n = p.length();
boolean[][] dp = new boolean[m+1][n+1];
dp[m][n] = true;
for (int i = n - 1; i >= 0; i--) {
if (p.charAt(i) == '*') {
dp[m][i] = true;
} else {
break;
}
}
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '?') {
dp[i][j] = dp[i+1][j+1];
} else if (p.charAt(j) == '*') {
dp[i][j] = dp[i+1][j] || dp[i][j+1]; // whether use * or not
} else {
dp[i][j] = false;
}
}
}
return dp[0][0];
}
}