-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path010-RegularExpressionMatching.js
More file actions
44 lines (38 loc) · 1.21 KB
/
010-RegularExpressionMatching.js
File metadata and controls
44 lines (38 loc) · 1.21 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
33
34
35
36
37
38
39
40
41
42
43
44
//-----------------------------------------------------------------------------
// Runtime: 276ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
var solution = function() {
'use strict';
/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function(s, p) {
return isMatchCore(s, 0, p, 0);
};
var isMatchCore = function(s, sIndex, p, pIndex) {
if (pIndex === p.length) { return sIndex === s.length; }
if (pIndex === p.length + 1 || p[pIndex + 1] !== '*') {
if (sIndex < s.length && (p[pIndex] === '.' || p[pIndex] === s[sIndex])) {
return isMatchCore(s, sIndex + 1, p, pIndex + 1);
} else {
return false;
}
} else {
while (sIndex < s.length && (p[pIndex] === '.' || p[pIndex] === s[sIndex])) {
if (isMatchCore(s, sIndex, p, pIndex + 2)) {
return true;
}
sIndex++;
}
return isMatchCore(s, sIndex, p, pIndex + 2);
}
};
return {
isMatch: isMatch
};
};
module.exports = solution();