Solution works on my local machine, but not leetcode.


  • 0
    Q

    404 / 439 test cases passed

    Input: "aa", "a*"

    Output: false

    Expected: true

    This case works on my local machine, but I couldn't figure out why it fails on leetcode.

    Here is my code

    bool isMatch(const char *s, const char *p) {
        
        const char *pp = NULL;
        const char *ss = NULL;
        char prev;
        const char *head = p;
        while (*s != '\0') {
            if (*p == '.' || (*s == *p && *p != '*' && *(p + 1) != '*')) {
                s++;
                p++;
            } else if (*p == '*' || *s == *p && *(p + 1) == '*') {
                if (*p == '*') {
                    if (head == p) {
                        return false;
                    }
                    prev = *(p - 1);
                    s--;
                } else {
                    prev = *p;
                }
    
                while (*(p + 1) == '*') {
                    p++;
                }
                    
                ss = s;
                pp = p;
                p ++;
            } else {
    
                if (*(p + 1) == '*') {
                    
                    while (*(p + 1) == '*') {
                        p ++;
                    }
                    p++;
                } else if (pp != NULL) {
                    if (prev == *ss || prev == '.') {
                        ss = ss + 1;
                        s = ss;
                        p = pp + 1;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        }
        
        while (*p != '\0') {
            if (*p != '*' && *(p + 1) == '*') {
                p = p + 2;
            } else if (*p == '*') {
                p++;
            } else {
                break;
            }
        }
        
        if (*p == '\0') {
            return true;
        }
        
        return false;
    }

  • 0
    S

    Hope faq might help you .


  • 0
    Q

    I am using the same version of g++ 4.7.3 on my local machine.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.