-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution10.java
More file actions
executable file
·26 lines (26 loc) · 1 KB
/
Copy pathSolution10.java
File metadata and controls
executable file
·26 lines (26 loc) · 1 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
class Solution10 {
public boolean isMatch(String s, String p) {
int len1 = s.length(), len2 = p.length();
boolean[][] res = new boolean[len1 + 1][len2 + 1];
// 边界条件
res[0][0] = true;
// 动态规划
for (int i = 0; i <= len1; i++)
for (int j = 1; j <= len2; j++){
if (p.charAt(j - 1) != '.' && p.charAt(j - 1) != '*'){
if (i != 0)
res[i][j] = res[i - 1][j - 1] && p.charAt(j - 1) == s.charAt(i - 1);
}
if (p.charAt(j - 1) == '.'){
if (i != 0)
res[i][j] = res[i - 1][j - 1];
}
if (p.charAt(j - 1) == '*'){
res[i][j] = res[i][j - 2];
if (i != 0 && (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.'))
res[i][j] = res[i - 1][j] || res[i][j - 2];
}
}
return res[len1][len2];
}
}