Skip to content

Commit

Permalink
Create: 10-Regular-Expression-Matching.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ykhan799 authored Sep 7, 2022
1 parent f9e95be commit 04066fa
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions typescript/10-Regular-Expression-Matching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function isMatch(s: string, p: string): boolean {
let lenS = s.length;
let lenP = p.length;
let map = {};

return check(0, 0);

function check(idxS: number, idxP: number): boolean {
if (map[idxS + ':' + idxP] !== undefined) {
return map[idxS + ':' + idxP];
}

if (idxS > lenS) {
return false;
}

if (idxS === lenS && idxP === lenP) {
return true;
}

if (p[idxP] === '.' || p[idxP] === s[idxS]) {
map[idxS + ':' + idxP] = p[idxP + 1] === '*' ? check(idxS + 1, idxP) || check(idxS, idxP + 2) : check(idxS + 1, idxP + 1);
}

else {
map[idxS + ':' + idxP] = p[idxP + 1] === '*' ? check(idxS, idxP + 2) : false;
}

return map[idxS + ':' + idxP];
}
};

0 comments on commit 04066fa

Please sign in to comment.