Implement wildcard pattern matching with support for '?'
and '*'
.
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false
1:注意特殊情况;2:遇到‘?',分别++;3:遇到‘*’的情况
bool isMatch(const char *s, const char *p)
{
if(s == NULL || p == NULL)
{
return false;
}
bool flag = false;
const char* indexP = nullptr;
const char* indexS = nullptr;
while( *s != '\0')
{
if(*p == '?')
{
p++;
s++;
}
else if(*p == '*')
{
while(*p == '*')
{
p++;
}
p--;
indexP = p;
indexS = s;
flag = true;
p++;
}
else
{
if(*p == *s)
{
p++;
s++;
}
else
{
if(flag == true)
{
p = indexP + 1;
s = ++indexS;
}
else
{
return false;
}
}
}
}
while(*p == '*')
{
p++;
}
if(*s == '\0' && *p == '\0')
{
return true;
}
else
{
return false;
}
}