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
参考链接:http://blog.csdn.net/hopeztm/article/details/8039777
代码:
class Solution {
public:
bool isMatch(const char *s, const char *p) {
int i;
bool star = false;
startLoop:
{
for( i = 0; s[i] ; i++){
switch(p[i])
{
case '?':
break;
case '*':
star = true;
p += i;
s += i;
while(*p == '*'){++p;}
if(!(*p)) return true;
goto startLoop;
default:
if(s[i] != p[i]){
goto starCheck;
}
}
}
}
while(p[i] == '*')i++;
return !(p[i]);
starCheck:
{
if(star){
s++;
goto startLoop;
}
return false;
}
}
};