I have a non-recursive solution to this problem. It works when I run on VS2012 (at least for the test cases I tried), but it gives different output for some cases, such as "aa", "a*".
could anyone look at the following code, and figure out the errors there? Thanks.
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if (s == NULL || p == NULL) return false;
const char *pstar = NULL;
const char *pss = NULL;
while (*s || *p) {
if (*s == *p || (*p == '.' && *s)) { // match found, move to next
s++;
p++;
}
else if (*p == '*') {
pstar = p;
pss = s;
s = s - 1; // * match zero characters
p = p + 1;
}
else if (*(p + 1) == '*') {
p = p + 2;
}
else if (pstar && (*(pstar - 1) == *(pss - 1) || (*(pstar - 1) == '.') && *s!='\0')){
s = pss;
pss++;
p = pstar + 1;
}
else {
return false;
}
}
return true;
}
};