Regular Expression Matching

Implement regular expression matching with support for '.' and '*'.

'.' 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") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

思路:

代码:

 1     bool isMatch(const char *s, const char *p) {
 2         // IMPORTANT: Please reset any member data you declared, as
 3         // the same Solution instance will be reused for each test case.  
 4         if(*p == '')
 5             return *s == '';
 6         if(*(p+1) == '*'){
 7             for(const char *t = s; *t != '' && (*t == *p || *p == '.'); t++){
 8                 if(isMatch(t+1, p+2))
 9                     return true;
10             }
11             return isMatch(s, p+2);
12         }
13         else{
14             if(*s != '' && (*s == *p || *p == '.'))
15                 return isMatch(s+1, p+1);
16             return false;
17         }
18     }
原文地址:https://www.cnblogs.com/waruzhi/p/3441863.html