一、题目描述
给定一个字符串s和一个模板p,判断他们是否匹配,其中
'.' Matches any single character. '*' Matches zero or more of the preceding element.
二、思路
1.p为空串,若s也为空串,那么返回true,否则返回false
2.p不是空串
1)p[1] == '*'
2)其他
三、C代码
class Solution {
public:
bool isMatch(string s, string p) {
if(p.empty()) return s.empty();
if(p[1] == '*' && p.size() > 1)
return isMatch(s, p.substr(2)) || (!s.empty() && (p[0] == '.' || p[0] == s[0])) && isMatch(s.substr(1), p);
else
return !s.empty() && isMatch(s.substr(1), p.substr(1)) && (p[0] == s[0] || p[0] == '.');
}
};
参考:https://www.cnblogs.com/grandyang/p/4461713.html