leetCode-Record icon indicating copy to clipboard operation
leetCode-Record copied to clipboard

面试题19. 正则表达式匹配

Open fireairforce opened this issue 5 years ago • 0 comments

这题我用递归写的:

/**
 * @param {string} s
 * @param {string} p
 * @return {boolean}
 */
var isMatch = function(s, p) {
  if (!p.length) {
    return !s.length;
  }
  let first_match = s.length && (s[0] === p[0] || p[0] === ".");
  if (p.length >= 2 && p[1] === "*") {
    return isMatch(s, p.slice(2)) || (first_match && isMatch(s.slice(1), p));
  } else return first_match && isMatch(s.slice(1), p.slice(1));
};

// console.log(isMatch("aa", "a"));

fireairforce avatar Feb 20 '20 13:02 fireairforce