-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRegularExpressionMatching.java
More file actions
28 lines (27 loc) · 1.02 KB
/
RegularExpressionMatching.java
File metadata and controls
28 lines (27 loc) · 1.02 KB
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
package problem001_010;
/**
* 010. Regular Expression Matching
*/
public class RegularExpressionMatching {
public boolean isMatch(String s, String p) {
int m = s.isEmpty()? 1: s.length() + 1;
int n = p.isEmpty()? 1: p.length() + 1;
boolean[][] dp = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 && j == 0) {
dp[i][j] = true;
} else if (i == 0) {
dp[i][j] = p.charAt(j-1) == '*' && dp[i][j - 2];
} else if (j == 0) {
dp[i][j] = false;
} else {
dp[i][j] = (dp[i-1][j-1] && (p.charAt(j-1) == s.charAt(i-1) || p.charAt(j-1) == '.')) ||
(dp[i][j-2] && p.charAt(j-1) == '*') ||
(dp[i-1][j] && p.charAt(j-1) == '*' && (s.charAt(i-1) == p.charAt(j-2) || p.charAt(j-2) == '.'));
}
}
}
return dp[m-1][n-1];
}
}