Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created June 15, 2020 03:53
Show Gist options
  • Save RP-3/d59b9efd0221992d9e9d0db5d4c87ac5 to your computer and use it in GitHub Desktop.
Save RP-3/d59b9efd0221992d9e9d0db5d4c87ac5 to your computer and use it in GitHub Desktop.
/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function(s, p) {
const memo = new Array(s.length+1).fill(0).map(() => new Array(p.length).fill(-1));
const r = (i, j) => {
if(i === s.length && j === p.length) return true;
if(j === p.length) return false;
if(memo[i][j] !== -1) return memo[i][j];
if(p[j] === '*'){
if(i === s.length) return r(i, j+1); // skip the star and match it against ''
return memo[i][j] = (
r(i+1, j+1) || // use the star to match just this char
r(i+1, j) || // use the star to match this and more
r(i, j+1) // skip the star and match it against ''
);
}
if(i === s.length) return false;
if(p[j] === '?') return memo[i][j] = r(i+1, j+1);
if(s[i] === p[j]) return memo[i][j] = r(i+1, j+1);
return false;
};
return r(0, 0);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment