class Solution {
public:
bool isMatchCore(string s, string p,int indexS,int indexP){
if (s[indexS] == ' ' && p[indexP] == ' ')
return true;
if (s[indexS] != ' ' && p[indexP] == ' ')
return false;
if (p[indexP + 1] == '*')
{
if (s[indexS] == p[indexP] || (p[indexP] == '.'&&s[indexS] != ' '))
return isMatchCore(s, p, indexS, indexP + 2) || isMatchCore(s, p, indexS + 1, indexP + 2)
|| isMatchCore(s, p, indexS + 1, indexP);
else
{
isMatchCore(s, p, indexS, indexP + 2);
}
}
if (p[indexP] == '.'&&s[indexS] != ' ')
return isMatchCore(s, p, indexS + 1, indexP + 1);
if (p[indexP]==s[indexS])
return isMatchCore(s, p, indexS + 1, indexP + 1);
return false;
}
bool isMatch(string s, string p) {
if (s == "" && p == "")
return false;
int indexS = 0,indexP = 0;
return isMatchCore(s, p, indexS, indexP);
}
};
显示超时了,暂时还没想到解决办法。