Translation
Implementation support "." Matches the regular expression of "*". The "." Match supports a single character "*" that matches 0 or more preceding element matches should overwrite the entire input string (rather than the local one). The prototype of the function should be:BOOLIsMatch (Const Char* S,Const Char* p) Example: IsMatch ("AA","a") →falseIsMatch ("AA","AA") →trueIsMatch ("AAA","AA") →falseIsMatch ("AA","A *") →trueIsMatch ("AA",".*") →trueIsMatch ("AB",".*") →trueIsMatch ("AaB","C*a*b") →true
Original
Implement Regular expression matching withSupport for '. ' and ' * '.'. 'Matches anySinglecharacter.' * 'MatchesZero orMore of thePrecedingelement. The matching should cover theEntire inputstring( notpartial). The function prototype should be:BOOL IsMatch (constChar*s, constChar*P) Some Examples:ismatch ("AA","a") →falseIsMatch ("AA","AA") →trueIsMatch ("AAA","AA") →falseIsMatch ("AA","A *") →trueIsMatch ("AA",".*") →trueIsMatch ("AB",".*") →trueIsMatch ("AaB","C*a*b") →true
On the internet to find ideas ... I didn't read the title.
Public classsolution{ Public BOOL IsMatch(stringSstringP) {if(P.length = =0)returnS.length = =0;if(P.length = =1)return(S.length = =1) && (p[0] = = s[0] || p[0] =='. ');if(p[1] !=' * ') {if(S.length = =0)return false;Else return(s[0] = = p[0] || p[0] =='. ') && IsMatch (s.substring (1), P.substring (1)); }Else{ while(S.length >0&& (p[0] = = s[0] || p[0] =='. ')) {if(IsMatch (S, p.substring (2)))return true; s = s.substring (1); }returnIsMatch (S, p.substring (2)); } }}
Copyright NOTICE: This article is nomasp Couvant original article, without permission is prohibited reprint! Welcome to my blog: http://blog.csdn.net/nomasp
Leetcode Regular expression Matching (regular expression matching)