19 lines c++ code of Regular Expression Matching (ideas from Rob Pike)


  • 0
    H
    class Solution {
    public:
        bool isMatch(const char *s, const char *p) {
            if (p[0] == '\0')
                return *s == '\0';
            if (p[1] == '*')
                return matchStar(p[0], s, p+2);
            if (*s != '\0' && (p[0] == '.' || p[0] == *s))
                return isMatch(s+1, p+1);
            return false;
        }
        bool matchStar(char c, const char *text, const char *pattern) {
            do {
                if (isMatch(text, pattern))
                    return true;
            } while(*text != '\0' && (*text++ == c || c == '.'));
            return false;
        }
    };

  • 0
    Y

    When I test 'a.' and 'aa', the result is false.
    I think '.' matches the second 'a', so the answer shuold be true.

    Then 'aa' and 'a.' is true.


Log in to reply
 

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