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 the entire input string (not partial).
Note:
-
s
could be empty and contains only lowercase lettersa-z
. -
p
could be empty and contains only lowercase lettersa-z
, and characters like.
or*
.
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 (.)".
Example 4:
Input: s = "aab" p = "c*a*b" Output: true Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".
Example 5:
Input: s = "mississippi" p = "mis*is*p*." Output: false
思路一:递归思路,先匹配第一个字符。终止条件:p到达末尾而s没有达到,返回false。
dfs(i, j)表示从s的第i个字符和p的第j个字符匹配。
Top-down
1 class Solution { 2 public: 3 bool isMatch(string s, string p) { 4 if (p.empty()) 5 return s.empty(); 6 bool first_match = (!s.empty() && (s[0] == p[0] || p[0] == '.')); //判断s和p的第一个字符是否匹配 7 if (p.length() >= 2 && p[1] == '*') { //如果p前两个字符是[a-z|.]*,则选择s的第一个字符是否与其匹配 8 return isMatch(s, p.substr(2)) || (first_match && isMatch(s.substr(1), p)); //前面那种情况选择直接跳过 9 } else { 10 return first_match && isMatch(s.substr(1), p.substr(1)); //p首字符不是[a-z|.]*这种情况 11 } 12 } 13 };
思路二:记忆化搜索
1 class Solution { 2 public: 3 bool isMatch(string s, string p) { 4 int lens = s.length(), lenp = p.length(); 5 vector<vector<int> > memo(lens + 1, vector<int>(lenp + 1, -1)); 6 return isMatch(s, p, 0, 0, memo); 7 } 8 bool isMatch(string s, string p, int i, int j, vector<vector<int> > &memo) { 9 if (memo[i][j] != -1) { 10 return memo[i][j]; 11 } 12 int ans; 13 if (j == p.length()) { 14 ans = (i == s.length()); 15 } else { 16 bool first_match = (i < s.length() && (p[j] == '.' || s[i] == p[j])); 17 if (j + 1 < p.length() && p[j + 1] == '*') { 18 ans = isMatch(s, p, i, j + 2, memo) || (first_match && isMatch(s, p, i + 1, j, memo)); 19 } else { 20 ans = first_match && isMatch(s, p, i + 1, j + 1, memo); 21 } 22 } 23 memo[i][j] = ans; 24 return ans; 25 } 26 };
思路三:动态规划
基于思路二,转化成迭代形式。dp[i][j]表示从下标i开始的s子串与从下标j开始的p子串是否匹配
1 class Solution { 2 public: 3 bool isMatch(string s, string p) { 4 int lens = s.length(), lenp = p.length(); 5 vector<vector<bool> > dp(lens + 1, vector<bool>(lenp + 1, false)); 6 dp[lens][lenp] = true; 7 for (int i = lens; i >= 0; i--) { 8 for (int j = lenp - 1; j >= 0; j--) { 9 bool first_match = (i < lens) && (p[j] == '.' || (s[i] == p[j])); 10 if (j + 1 < lenp && p[j + 1] == '*') { 11 dp[i][j] = dp[i][j + 2] || (first_match && dp[i + 1][j]); 12 } else { 13 dp[i][j] = first_match && dp[i + 1][j + 1]; 14 } 15 } 16 } 17 return dp[0][0]; 18 } 19 };
还有另一种形式的dp,dp[i][j]表示长度为i的s和长度为j的p是否匹配。
1 class Solution { 2 public: 3 bool isMatch(string s, string p) { 4 int lens = s.length(), lenp = p.length(); 5 vector<vector<bool> > dp(lens + 1, vector<bool>(lenp + 1, false)); 6 dp[0][0] = true; 7 for (int i = 0; i <= lens; i++) { 8 for (int j = 1; j <= lenp; j++) { 9 if (p[j - 1] == '*') { 10 dp[i][j] = dp[i][j - 2] || (i > 0 && (s[i - 1] == p[j - 2] || p[j - 2] == '.') && dp[i - 1][j]); 11 } else { 12 dp[i][j] = i > 0 && (s[i - 1] == p[j - 1] || p[j - 1] == '.') && dp[i - 1][j - 1]; 13 } 14 } 15 } 16 return dp[lens][lenp]; 17 } 18 };