From 04066fadcbcfe674289c87aad57b174faf55fd60 Mon Sep 17 00:00:00 2001 From: Yaseen Khan <78000116+Ykhan799@users.noreply.github.com> Date: Tue, 6 Sep 2022 21:30:23 -0700 Subject: [PATCH] Create: 10-Regular-Expression-Matching.ts --- typescript/10-Regular-Expression-Matching.ts | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 typescript/10-Regular-Expression-Matching.ts diff --git a/typescript/10-Regular-Expression-Matching.ts b/typescript/10-Regular-Expression-Matching.ts new file mode 100644 index 000000000..301e4552f --- /dev/null +++ b/typescript/10-Regular-Expression-Matching.ts @@ -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]; + } +}; \ No newline at end of file