Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save luoxiaoxun/5947278 to your computer and use it in GitHub Desktop.

Select an option

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…
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