Description
Given an input string s
and a pattern p
, implement regular expression matching with support for '.'
and '*'
where:
'.'
Matches any single character.'*'
Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
Example 1:
Input: s = “aa”, p = “a” Output: false Explanation: “a” does not match the entire string “aa”.
Example 2:
Input: s = “aa”, p = “a*” Output: true Explanation: ’*’ means zero or more of the preceding element, ‘a’. Therefore, by repeating ‘a’ once, it becomes “aa”.
Example 3:
Input: s = “ab”, p = ”.*” Output: true Explanation: ”.*” means “zero or more (*) of any character (.)“.
Constraints:
1 <= s.length <= 20
1 <= p.length <= 20
s
contains only lowercase English letters.p
contains only lowercase English letters,'.'
, and'*'
.- It is guaranteed for each appearance of the character
'*'
, there will be a previous valid character to match.
Code
Top Down DP with memo
- 對於 index i 來說
- 如果目前字串(s)和 regex(p)相同,比較 index i + 1 之後的部分
dp[i][j] = dp[i + 1][j + 1]
- 如果 p 目前是 . 代表可以 match 任何字母,所以一樣比較 index i + 1 之後的部分
dp[i][j] = dp[i + 1][j + 1]
- 如果 p 在 i + 1 的部分是 star ,一個 star 代表可以 match 0 或是多個 preceding character,例如
a*
代表可以 match 0 或是多個 a,因此分成兩種 case- match 0: 比較 i + 2 之後的部分
dp[i][j] = dp[i][j + 2]
- match 1 ~ n:比較 i + 1, i + 2, i + 3 … … 之後的部分
dp[i][j] = dp[i + 1][j + 2] || dp[i + 2][j + 2] || dp[i + 3][j + 2] || ......
- match 0: 比較 i + 2 之後的部分
- 如果目前字串(s)和 regex(p)相同,比較 index i + 1 之後的部分
Bottom Up
和 Edit Distance 以及 Distinct Subsequences 類似。
Time Complexity: , Space Complexity:
注意 for(int i = m ; i >= 0; i--)
是從 i = m
開始,以 s = "aa", p = "a*
去 dry run 就會知道為何。