Runtime error in a simple recursive solution. Any idea why?


  • 0
    M

    Runtime error on below input. works fine on local.

    s = "acaabbaccbbacaabbbb"
    p = "a*.b.aaaa"

    class Solution {
    public:
    bool MatchSingle(char a, char b)
    {
    return ((b == '.') || (a == b));
    }

    bool isMatch(const char *s, const char *p) {
        if(*p == '\0' && *s == '\0')
        {
            return true;
        }
        else if(*p == '\0')
        {
            return false;
        }
        
        if(*(p+1) == '*')
        {
            return isMatch(s, p+2) || (MatchSingle(*s, *p) && isMatch(s+1, p));
        }
        return MatchSingle(*s, *p) && isMatch(s+1, p+1);
    }
    

    };


  • 0
    P

    In fact, when the situation is " *s=='\0' and (p+1)=='' ", then isMatch(s+1,p) will be out of space! so, i think you could add a judgment " *s=='\0'?s:s+1 "


Log in to reply
 

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