-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpressionMatching.java
More file actions
32 lines (26 loc) · 1.1 KB
/
Copy pathRegularExpressionMatching.java
File metadata and controls
32 lines (26 loc) · 1.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
27
28
29
30
31
32
enum State { TRUE, FALSE }
class Solution {
State[][] cache;
public boolean isMatch(String text, String pattern) {
cache = new State[text.length() + 1][pattern.length() + 1];
return dfs(0, 0, text, pattern);
}
private boolean dfs(int tIdx, int pIdx, String text, String pattern) {
if (cache[tIdx][pIdx] != null)
return cache[tIdx][pIdx] == State.TRUE;
boolean matched;
if (pIdx == pattern.length()) {
matched = tIdx == text.length();
} else {
boolean first = (tIdx < text.length() &&
(pattern.charAt(pIdx) == text.charAt(tIdx) || pattern.charAt(pIdx) == '.'));
if (pIdx + 1 < pattern.length() && pattern.charAt(pIdx + 1) == '*')
matched = dfs(tIdx, pIdx + 2, text, pattern) ||
(first && dfs(tIdx + 1, pIdx, text, pattern));
else
matched = first && dfs(tIdx + 1, pIdx + 1, text, pattern);
}
cache[tIdx][pIdx] = matched ? State.TRUE : State.FALSE;
return matched;
}
}