Welcome to LeetCode Discuss.  Please read the FAQ to help yourself making the best use of Discuss.
Ask a Question
Back to Problem

Welcome to LeetCode Discuss.

This is a place to ask questions related to only OJ problems.

Please read the FAQ to help yourself making the best use of Discuss.

Different output from OJ and VS2012

0 votes
93 views

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;
}
 };
asked Dec 14, 2013 in Regular Expression Matching by gccheng (120 points)

Could you please update your post with some explanation of your algorithm, and make some comment in your code?

1 Answer

0 votes

I have no idea what your algorithm is, so do not know if it right.

However I could tell now, when run the test case "aa", "a*", your *p will go out of string range, you can simulate by yourself on paper. so there is no guarantee about what result will return. Maybe it leads to different judgement between local and LeetCode OJ.

answered Dec 16, 2013 by Shangrila (21,210 points)

...