‘.‘ Matches any single character. ' * ' Matches zero or more of the preceding element. The matching should cover the 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
The original question above
Leetdcode to the title of the mark is a DP, although DP can do, but I really do not think that DP is a very intuitive method. Well, I'm a normal young man, the hardest part of the problem is backtracking, backtracking, but I'm stuck here.
For a match, a very intuitive approach is to greedy, as much as possible to match more characters. However, because * this character may match 0, 1 or more, resulting in exactly how many times the greedy algorithm is not very well controlled. What if the "*" Match puzzle is resolved?
We still match the characters from beginning to end, and for the processing of ' * ' We are as follows:
If the next character of P is not ' * ' and then it must match the current character of s. Continue pattern matching with the next character of both s and p.
If the next character of P are ' * ', then we do a brute force exhaustive matching of 0, 1, or more repeats of Curre NT character of p... Until we could not match any more characters.
If the next character is ' * ', then we should match 0 times, 1 times, do a brute force solution several times, and consider all the circumstances. In each case, it is solved in a recursive way.
Since it is recursive, it is necessary to consider base well. I think this recursive base should be a match string and the matching string has all been scanned.
Int ismatch (char* s, char* p) { assert (s && p); //s and p are null if (*p == ') { return *s == '; ' } if (* (p+1) != ' * ') //next character is not ' * ' { assert (*p != ' * '); if ((*p == *s) | | (*p == '. ') && *s != ')) { return ismatch (s+1, p+1); }else{ return&nbsP;0; } }else{ //next character is ' * ', match 0, 1, 2,... times while ((*p == *s) | | (*p == '. ') && *s != ')) { if (IsMatch (s, p+2)) { return 1; } s++; } return ismatch (s), p+2); }}
Leetcode--regular Expression Matching