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 (constcharconstchar* 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
BOOLIsMatch (Const Char* S,Const Char*p) { if(s = = NULL | | p = =NULL)return false; if(*p = =' /') return(sb==' /'; //next char is not ' * ' if(* (p+1) !='*') return((*p = = *s) | | (*p = ='.'&& *s! =' /')) && IsMatch (s+1, p+1); //Next char is ' * ' while((*p = = *s) | | (*p = ='.'&& *s! =' /')) { if(IsMatch (S, p+2)) return true; S++; } returnIsMatch (S, p+2);}
Http://leetcode.com/2011/09/regular-expression-matching.html
Regular Expression Matching