Title Link: https://leetcode.com/problems/regular-expression-matching/
Implement regular expression matching with support for ‘.‘
and ‘*‘
.
entire input string (not partial). The function prototype should be:bool IsMatch (const char *s, const char *p) Some examples:ismatch ("AA", "a") →falseismatch ( "AA", "AA") →trueismatch ("AAA", "AA") →falseismatch ("AA", "A *") →trueismatch ("AA", ". *") →trueismatch ("AB", ". *") →true IsMatch ("AaB", "C*a*b") →true
BOOL IsMatch (char* S, char* p) { if (*p = = ' && ' *s ') return true; if (*p && * (p + 1) = = ' * ') { //if P is followed by *, the matching failure requires backtracking if (*p! = '. ' && *s! = *p | | *s = = ' + ') //If the current word Characters do not match, * can only take 0, remove the first two characters of P and then recursively match return IsMatch (S, p + 2); if (IsMatch (s, p + 2)) //fetch * is 0 times, if can match, return match succeeded; return true; Return IsMatch (S + 1, p); Otherwise backtracking, remove the s initials (* at least once) to continue the recursive match } else if (*p = = '. ' && *s! = ') ') | | *p = = *s) //current character matching, recursive comparison sequence; C15/>return IsMatch (S + 1, p + 1); else return false;} Note '. ' does not match all characters, because it is possible to '/';
#10 Regular Expression Match