-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathisMatch.java
More file actions
25 lines (24 loc) · 819 Bytes
/
isMatch.java
File metadata and controls
25 lines (24 loc) · 819 Bytes
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
public boolean isMatch(String s, String p) {
boolean[][] T = new boolean[s.length()+1][p.length()+1];
T[0][0] = true;
for (int i = 1; i < T[0].length; i++) {
if (p.charAt(i-1) == '*') {
T[0][i] = T[0][i - 2];
}
}
for (int i = 1; i < T.length; i++) {
for (int j = 1; j < T[0].length; j++) {
if (p.charAt(j-1) == '.' || p.charAt(j-1) == s.charAt(i-1)) {
T[i][j] = T[i-1][j-1];
} else if (p.charAt(j-1) == '*') {
T[i][j] = T[i][j - 2];
if (p.charAt(j-2) == '.' || p.charAt(j-2) == s.charAt(i-1)) {
T[i][j] = T[i][j] | T[i - 1][j];
}
} else {
T[i][j] = false;
}
}
}
return T[s.length()][p.length()];
}