Regular Expression Matching

Given an input string (s) and a pattern (p), implement regular expression matching with support for'.'and'*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover theentireinput string (not partial).

Note:

  • scould be empty and contains only lowercase lettersa-z.

  • pcould be empty and contains only lowercase lettersa-z, and characters like . or*.

Example

Example 1:

Input:

s = "aa"
p = "a"

Output:
 false

Explanation:
 "a" does not match the entire string "aa".

Example 2:

Example 3:

Example 4:

Example 5:

Note

分类讨论:

  1. 当前字符相同的情况:sChar == pChar || pChar == '.' 相同或者pattern字符是".",s和p都看下一个字符开头的子串

  2. p的下一个字符是"*"的情况:

    • p向后跳2位,对应"a*"是空的情况

    • s和p当前字符相同,并且s往后跳一个,对应一个或者重复的情况

递归边界:

  1. ““ == ”“ 的情况:p到头,s也要到头

  2. “” == "x*x*x*x*x*" 的情况,s到头,p可以为空 isEmpty(p, pIndex)

Memo:

  1. 二维boolean数组表示0到index的子串匹配情况

  2. visited数组来记录是否在memo里出现

Code

Last updated