-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegular-Expression-II.js
42 lines (34 loc) · 1.07 KB
/
Regular-Expression-II.js
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
function solve(S, P){
let dp = new Map();
function isCheck(i, j) {
if (i < 0 && j < 0) {
return 1;
} else if (j < 0) {
return 0;
} else if (i < 0) {
for (let k = j; k >= 0; k -= 2) {
if (P[k] !== '*') {
return 0;
}
}
return 1;
}
if (dp.has(`${i}${j}`)) {
return dp.get(`${i}${j}`);
}
if (S[i] == P[j] || P[j] == ".") {
let val = isCheck(i - 1, j - 1);
dp.set(`${i}${j}`, val);
} else if (P[j] == "*") {
// Case 1: '*' matches zero occurrences of the preceding character in P
// Case 2: '*' matches one or more occurrences of the preceding character in P
let val = (isCheck(i, j - 2) || (S[i] == P[j - 1] || P[j - 1] == '.') && isCheck(i - 1, j));
dp.set(`${i}${j}`, val);
} else {
dp.set(`${i}${j}`, 0);
}
return dp.get(`${i}${j}`);
}
return isCheck(S.length - 1, P.length - 1)?1:0;
}
console.log(solve('baaaaaabaaaabaaaaababababbaab', '..a*aa*a.aba*a*bab*'));