Skip to content

Commit 27fc006

Browse files
authored
Merge pull request #735 from kciccolella/leetcode10
Create 10-Regular-Expression-Matching.js
2 parents b9e894a + 71a2bbe commit 27fc006

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} p
4+
* @return {boolean}
5+
*/
6+
var isMatch = function(s, p) {
7+
var lenS = s.length;
8+
var lenP = p.length;
9+
var map = {};
10+
11+
return check(0, 0);
12+
13+
function check(idxS, idxP) {
14+
if (map[idxS + ':' + idxP] !== undefined) {
15+
return map[idxS + ':' + idxP];
16+
}
17+
18+
if (idxS > lenS) {
19+
return false;
20+
}
21+
22+
if (idxS === lenS && idxP === lenP) {
23+
return true;
24+
}
25+
26+
if (p[idxP] === '.' || p[idxP] === s[idxS]) {
27+
map[idxS + ':' + idxP] = p[idxP + 1] === '*' ?
28+
check(idxS + 1, idxP) || check(idxS, idxP + 2) :
29+
check(idxS + 1, idxP + 1);
30+
} else {
31+
map[idxS + ':' + idxP] = p[idxP + 1] === '*' ?
32+
check(idxS, idxP + 2) : false;
33+
}
34+
35+
return map[idxS + ':' + idxP];
36+
}
37+
};

0 commit comments

Comments
 (0)