10.Regular Expression Matching
问题概述
Given an input string (s) and a pattern §, 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 letters a-z
.
p
could be empty and contains only lowercase letters a-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 precedeng 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
分析
首先要理解题意:
“a"对应"a”, 任意字母对应".",
0到多个相同字符x,对应"x*", 比起普通正则,这个地方多出来一个前缀x. x代表的是 相同的字符中取一个,比如"aaaab"对应是"ab"
"“还有一个易于疏忽的地方就是它的"贪婪性"要有一个限度.比如"aaa"对应"a*a”, 代码逻辑不能一路贪婪到底。
解题过程
1、考虑特殊情况即*s字符串或者*p字符串结束。
(1)s字符串结束,要求*p也结束或者间隔‘’ (例如p=”a*b*c……”),否则无法匹配
(2)*s字符串未结束,而*p字符串结束,则无法匹配
2、*s字符串与*p字符串均未结束
(1)(p+1)字符不为*,则只需比较s字符与*p字符,若相等则递归到(s+1)字符串与*(p+1)字符串的比较,否则无法匹配。
(2)(p+1)字符为*,则p字符可以匹配*s字符串中从0开始任意多(记为i)等于*p的字符,然后递归到(s+i+1)字符串与*(p+2)字符串的比较,
只要匹配一种情况就算完全匹配。
代码
class Solution {
public:
bool isMatch(string s, string p) {
//判断参数合法,以及程序正常结束
assert( s && p);
if(*p == '\0') return *s == '\0';
//next char is not '*'; must match current character
if(*(p+1) != '*')
{
assert(*p != '*');//考虑情况isMatch('aa','a*');
return ((*p == *s) ||(*p == '.' && *s != '\0')) && isMatch(s + 1, p + 1);
}
//next char is '*' 继续递归匹配,不能写成*(p+1) == '*' 考虑情况isMatch('ab','.*c')
while((*p == *s)|| (*p == '.' && *s != '\0'))
{
if (isMatch(s, p+2)) return true;
s++;
}
//匹配下一个模式
return isMatch(s,p+2);
}
};