forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
33 lines (31 loc) · 879 Bytes
/
Solution.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
31
32
33
class Solution {
private Boolean[][] f;
private String s;
private String p;
private int m;
private int n;
public boolean isMatch(String s, String p) {
m = s.length();
n = p.length();
f = new Boolean[m + 1][n + 1];
this.s = s;
this.p = p;
return dfs(0, 0);
}
private boolean dfs(int i, int j) {
if (j >= n) {
return i == m;
}
if (f[i][j] != null) {
return f[i][j];
}
boolean res = false;
if (j + 1 < n && p.charAt(j + 1) == '*') {
res = dfs(i, j + 2)
|| (i < m && (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.') && dfs(i + 1, j));
} else {
res = i < m && (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.') && dfs(i + 1, j + 1);
}
return f[i][j] = res;
}
}