题目:
题解:
bool allStars(char* str, int left, int right) {
for (int i = left; i < right; ++i) {
if (str[i] != '*') {
return false;
}
}
return true;
}
bool charMatch(char u, char v) { return u == v || v == '?'; };
bool isMatch(char* s, char* p) {
int len_s = strlen(s), len_p = strlen(p);
while (len_s && len_p && p[len_p - 1] != '*') {
if (charMatch(s[len_s - 1], p[len_p - 1])) {
len_s--;
len_p--;
} else {
return false;
}
}
if (len_p == 0) {
return len_s == 0;
}
int sIndex = 0, pIndex = 0;
int sRecord = -1, pRecord = -1;
while (sIndex < len_s && pIndex < len_p) {
if (p[pIndex] == '*') {
++pIndex;
sRecord = sIndex;
pRecord = pIndex;
} else if (charMatch(s[sIndex], p[pIndex])) {
++sIndex;
++pIndex;
} else if (sRecord != -1 && sRecord + 1 < len_s) {
++sRecord;
sIndex = sRecord;
pIndex = pRecord;
} else {
return false;
}
}
return allStars(p, pIndex, len_p);
}