-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.正则表达式匹配.js
56 lines (48 loc) · 1.06 KB
/
10.正则表达式匹配.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
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* @lc app=leetcode.cn id=10 lang=javascript
*
* [10] 正则表达式匹配
*/
// @lc code=start
/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function(s, p) {
const m = s.length;
const n = p.length;
const memo = new Array(m);
for(let i = 0; i < m; i++) {
memo[i] = new Array(n);
}
const dp = (s, i, p, j) => {
if (j === n) return i === m;
if (i === m) {
if ((n - j) % 2 === 1) return false;
for (; j < n; j+=2) {
if (p[j + 1] !== '*') return false;
}
return true;
}
if(memo[i][j] !== undefined) {
return memo[i][j];
}
if (s[i] === p[j] || p[j] === '.') {
if (j + 1 < n && p[j + 1] === '*') {
memo[i][j] = dp(s, i, p, j + 2) || dp(s, i + 1, p, j);
} else {
memo[i][j] = dp(s, i + 1, p, j + 1);
}
} else {
if (j + 1 < n && p[j + 1] === '*') {
memo[i][j] = dp(s, i, p, j + 2);
} else {
memo[i][j] = false;
}
}
return memo[i][j];
}
return dp(s, 0, p, 0);
};
// @lc code=end