-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0010.java
More file actions
52 lines (45 loc) · 1.68 KB
/
Copy pathLeetCode0010.java
File metadata and controls
52 lines (45 loc) · 1.68 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* Regular Expression Matching
* Input: s = "aab", p = "c*a*b"
* Output: true
* Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".
* */
import java.util.Arrays;
public class LeetCode0010 {
public static void main(String args[]) {
String s = "";
String p = "";
System.out.println(isMatch(s, p));
}
//https://leetcode-cn.com/problems/regular-expression-matching/solution/shou-hui-tu-jie-wo-tai-nan-liao-by-hyj8/
public static boolean isMatch(String s, String p) {
if (p.length() == 0 && s.length() == 0)
return true;
if (p.length() == 0 && s.length() != 0)
return false;
int slen = s.length();
int plen = p.length();
boolean[][] dp = new boolean[slen + 1][plen + 1];
dp[0][0] = true;
if (p.charAt(0) == '*')
p = p.substring(1, plen - 1);
for (int j = 1; j < plen + 1; j++) {
if (p.charAt(j - 1) == '*') dp[0][j] = dp[0][j - 2];
}
for (int i = 1; i < slen + 1; i++) {
for (int j = 1; j < plen + 1; j++) {
if (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '.') {
dp[i][j] = dp[i - 1][j - 1];
}
else if (p.charAt(j - 1) == '*') {
if (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.') {
dp[i][j] = dp[i][j - 2] || dp[i - 1][j - 2] || dp[i - 1][j];
}
else {
dp[i][j] = dp[i][j - 2];
}
}
}
}
return dp[slen][plen];
}
}