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
参考 :
https://www.cnblogs.com/boring09/p/4246055.html
class Solution
{
public:
bool isMatch(string s, string p)
{
int sbegin = 0;
int pbegin = 0;
int backUp = 0;
int start = 0;
bool Is = false;
while (sbegin < s.length())
{
if (p[pbegin] == '*')
{
Is = true;
backUp = sbegin;
start = ++pbegin;
}
else if (p[pbegin] == '?' || p[pbegin] == s[sbegin])
{
sbegin++;
pbegin++;
}
else if (Is && pbegin <= p.length())
{
pbegin = start;
sbegin = ++backUp;
}
else break;
}
while (pbegin < p.length() && p[pbegin] == '*') pbegin++;
return (pbegin == p.length() && sbegin == s.length());
}
};