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).
class Solution {
public:
bool isMatch( string s, string p)//判断是否匹配
{
if (p.empty())
return s.empty();
if (p.size() == 1)
{
return (s.size() == 1 && (s[0] == p[0] || p[0] == '.'));
}
if (p[1] != '*')
{
if (s.empty())
return false;
return (s[0] == p[0] || p[0] == '.') && isMatch(s.substr(1), p.substr(1));
}
while (!s.empty() && (s[0] == p[0] || p[0] == '.'))
{
if (isMatch(s, p.substr(2))) return true;
s = s.substr(1);
}
return isMatch(s, p.substr(2));
}
};