-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwildcard-matching.js
More file actions
40 lines (32 loc) · 900 Bytes
/
Copy pathwildcard-matching.js
File metadata and controls
40 lines (32 loc) · 900 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function(s, p) {
var dp = Array(p.length + 1).fill(0).map(_ => ({}));
return test(s, p, 0, 0, dp);
};
var test = function (s, p, sIndex, pIndex, dp) {
if (dp[pIndex][sIndex] !== undefined)
return dp[pIndex][sIndex];
var sNow = s[sIndex];
var pNow = p[pIndex];
var res = false;
if (pNow === undefined)
return sNow === undefined;
if (sNow === undefined) {
for (var i = pIndex; i < p.length; i++) {
if (p[i] !== '*')
return false;
}
return true;
}
if (sNow === pNow || pNow === '?') {
res = test(s, p, sIndex + 1, pIndex + 1, dp);
} else if (pNow === '*') {
res = test(s, p, sIndex, pIndex + 1, dp) || test(s, p, sIndex + 1, pIndex + 1, dp) || test(s, p, sIndex + 1, pIndex, dp);
}
dp[pIndex][sIndex] = res;
return res;
};