Skip to content

Commit 04066fa

Browse files
authored
Create: 10-Regular-Expression-Matching.ts
1 parent f9e95be commit 04066fa

File tree

1 file changed

+31
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)