Created
July 8, 2013 08:53
-
-
Save luoxiaoxun/5947278 to your computer and use it in GitHub Desktop.
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| C++: | |
| class Solution { | |
| public: | |
| bool isMatch(const char *s, const char *p) { | |
| // Start typing your C/C++ solution below | |
| // DO NOT write int main() function | |
| if(*p=='\0') return *s=='\0'; | |
| if(*(p+1)!='*') | |
| return ((*s==*p)||(*s!='\0'&&*p=='.'))&&isMatch(s+1,p+1); | |
| while((*s==*p)||(*s!='\0'&&*p=='.')){ | |
| if(isMatch(s,p+2)) break; | |
| s++; | |
| } | |
| return isMatch(s,p+2); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment