Skip to content

Commit

Permalink
Merge pull request neetcode-gh#735 from kciccolella/leetcode10
Browse files Browse the repository at this point in the history
Create 10-Regular-Expression-Matching.js
  • Loading branch information
Ahmad-A0 authored Aug 6, 2022
2 parents b9e894a + 71a2bbe commit 27fc006
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions javascript/10-Regular-Expression-Matching.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function(s, p) {
var lenS = s.length;
var lenP = p.length;
var map = {};

return check(0, 0);

function check(idxS, idxP) {
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 27fc006

Please sign in to comment.