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
分类讨论:
当前字符相同的情况:
sChar == pChar || pChar == '.'相同或者pattern字符是".",s和p都看下一个字符开头的子串p的下一个字符是"*"的情况:
p向后跳2位,对应"a*"是空的情况
s和p当前字符相同,并且s往后跳一个,对应一个或者重复的情况
递归边界:
““ == ”“的情况:p到头,s也要到头“” == "x*x*x*x*x*"的情况,s到头,p可以为空isEmpty(p, pIndex)
Memo:
二维boolean数组表示0到index的子串匹配情况
visited数组来记录是否在memo里出现
Code
Last updated