Leetcode Regular expression Matching (regular expression matching)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.