[Leetcode] #44 Wildcard Matching

Source: Internet
Author: User

Mplement wildcard pattern matching with the support for and ‘?‘ ‘*‘ .

‘?‘ Matches any single character. ' * ' Matches any sequence of characters (including the empty sequence). 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") →falseismatch ( "AA", "AA") →trueismatch ("AAA", "AA") →falseismatch ("AA", "*") →trueismatch ("AA", "A *") →trueismatch ("AB", "? *") →truei Smatch ("AaB", "C*a*b") →false

The question is the matching problem, if encountered '? ' Match any one character, if it's ' * ', match 0-n characters. The greedy algorithm is used to solve. Time: 20ms. The code is as follows:
classSolution { Public:    BOOLIsMatch (stringSstringp) {if(P.empty ())returnS.empty (); string:: Const_iterator pter = P.begin (), ster =S.begin (); string:: Const_iterator star_p, star_s; BOOLSign =false;  while(ster!=S.end ()) {            if(Pter = =P.end ()) {                if(sign) {ster= ++star_s; Pter=star_p; }                Else                     Break; }            Else if(*pter = ='?'|| *pter = = *ster)++pter, + +ster; Else if(*pter = ='*'){                 while(Pter! = P.end () && (*pter = ='*'|| *pter = ='?')){                    if(*pter = ='?'){                        ++Pter; ++ster; if(Ster = =s.end ()) Break; }                    Else++Pter; }                if(pter==p.end ())return true; Star_p=Pter; star_s=ster; Sign=true; }            Else if((*pter! = *ster | | *pter!='?') &&Sign ) {Ster= ++star_s; Pter=star_p; }            Else                return false; }        if(Ster! =s.end ())return false;  while(pter!=p.end ())if(*pter++! ='*')                return false; return true; }};

[Leetcode] #44 Wildcard Matching

Related Article

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.