10. 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
bool isMatch(char* s, char* p) {
int i = 0,j = 0;
for(;s[i] != '\0' && p[j] != '\0';i++,j++){
if(p[j] == '*'){
if(p[j-1] == '.'){
if(p[j+1] == '\0') return 1;
while(s[i] != '\0' && s[i] != p[j+1]) i++;
if(s[i] = '\0') return 0;
j++;
}
else{
while(s[i] == p[j-1]) i++;
while(p[j+1] == s[i-1]) j++;
i--;
}
}
else if(p[i] != '.' && s[i] != p[j]){
if(p[j+1] != '*') return 0;
j++,i--;
}
}
if(s[j] != '\0') return 0;
if(p[j] != '\0'){
while(p[j] == )
}
return 1;
}
Email:JingwangLi@outlook.com