Simple recursive solution in C, 8 lines of code


  • 0
    L
    bool isMatch(char* s, char* p) {
        if (*p == '\0')
            return *s == '\0';
        if (*(p + 1) == '*') {
            while (!isMatch(s, p + 2)) 
                if (*p == '.' ? *s++ == '\0' : *s++ != *p) return false;
            return true;
        }
        return (*s == *p || *p == '.' && *s != '\0') && isMatch(++s, ++p);
    }
    

  • 0
    M

    while (!isMatch(s, p + 2))
    if (*p == '.' ? *s++ == '\0' : *s++ != *p) return false;
    I don't understand why it is writed!


Log in to reply
 

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