剑指OFFER----19、正则表达式匹配(js实现)

题目

请实现一个函数用来匹配包括’.‘和’‘的正则表达式。模式中的字符’.‘表示任意一个字符,而’'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但是与"aa.a"和"ab*a"均不匹配


思想
当模式中的第二个字符不是“*”时:

  1. 如果字符串第一个字符和模式中的第一个字符相匹配,那么字符串和模式都后移一个字符,然后匹配剩余的。
  2. 如果 字符串第一个字符和模式中的第一个字符相不匹配,直接返回false。

而当模式中的第二个字符是“*”时:
如果字符串第一个字符跟模式第一个字符不匹配,则模式后移2个字符,继续匹配。如果字符串第一个字符跟模式第一个字符匹配,可以有3种匹配方式:

  1. 模式后移2字符,相当于x*被忽略;
  2. 字符串后移1字符,模式后移2字符;
  3. 字符串后移1字符,模式不变,即继续匹配字符下一位,因为*可以匹配多位;
function match(s, pattern) {
  // write code here
  if (s == null || pattern == null) {
    return false
  }
  return checkMatch(s, pattern, 0, 0)
}

function checkMatch(s, pattern, i, j) {
  if (i === s.length && j === pattern.length) {
    return true
  }
  if (j === pattern.length && i !== s.length) {
    return false
  }
  // 第二个符号为 *
  if (pattern[j + 1] && pattern[j + 1] === '*') {
    // 第一个字符匹配
    if (s[i] === pattern[j] || (pattern[j] === '.' && i !== s.length)) {
      return checkMatch(s, pattern, i, j + 2) || checkMatch(s, pattern, i + 1, j) || checkMatch(s, pattern, i + 1, j + 2)
    // 第一个字符不匹配
    } else {
      return checkMatch(s, pattern, i, j + 2)
    }
  }
  if (pattern[j] === s[i] || (pattern[j] === '.' && i !== s.length)) {
    return checkMatch(s, pattern, i + 1, j + 1)
  }
  return false
}

猜你喜欢

转载自blog.csdn.net/qq_40816360/article/details/95177517