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
Solution:
Code:
<span style="font-size:14px;">class Solution {
public:
bool isMatch(const char *s, const char *p) {
int indexS = 0, indexP = 0, preS = -1, preP = -1;
while (s[indexS] != '\0') {
if (s[indexS] == p[indexP] || p[indexP] == '?') {
++indexS;
++indexP;
continue;
}
if (p[indexP] == '*') {
preS = indexS;
preP = indexP;
++indexP;
continue;
}
if (preP != -1) {
++preS;
indexS = preS;
indexP = preP+1;
continue;
}
return false;
}
while (p[indexP] != '\0' && p[indexP] == '*') ++indexP;
return p[indexP] == '\0';
}
};</span>