[Leetcode] Wildcard Matching Wildcard Match

Source: Internet
Author: User

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

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

This question wildcard matching problem is still small difficult, this word with greedy algorithm greedy alogrithm to solve, because there are special characters * and? which Can replace any character, * can replace any string, then we need to define a few additional pointers, where scur and Pcur point to the currently traversed character, and then define Pstar point to the last position in P, Sstar point at the position of the corresponding s, the specific algorithm is as follows:

-Define Scur, Pcur, Sstar, Pstar

-If *scur exists

-If *scur equals *pcur or *pcur is '? ', then both Scur and pcur are self-increasing by 1

-If *pcur is ' * ', then Pstar points to pcur position, pcur self-increment 1, and Sstar points to scur

-If Pstar exists, pcur points to the next position of Pstar, Scur points to Sstar since 1

-If Pcur is ' * ', then pcur self-increment 1

-If *pcur exists, returns False if not present, returns true

classSolution { Public:    BOOLIsMatch (Const Char*s,Const Char*p) {Const Char*scur = s, *pcur = p, *sstar = NULL, *pstar =NULL;  while(*scur) {            if(*scur = = *pcur | | *pcur = ='?') {                ++scur; ++pcur; } Else if(*pcur = ='*') {Pstar= pcur++; Sstar=scur; } Else if(Pstar) {pcur= Pstar +1; Scur= ++Sstar; } Else return false; }          while(*pcur = ='*') ++pcur; return!*pcur; }};

This question to consider can use dynamic programming solution, began to write a two-dimensional DP, found that memory is super, and then changed to a one-dimensional DP, found that the last test case, a look at the case, a string of more than 30,000, OJ is still the same as the insane, see someone on the net with if conditions forcibly skip this case (see here), but I feel since OJ so memory and time, estimated that is not want us to use DP to solve, or good grasp the above method is better, finally or DP also posted out bar, Although the last test case cannot be passed:

//cannot pass the last test caseclassSolution { Public:    BOOLIsMatch (Const Char*s,Const Char*p) {intm = strlen (s), n =strlen (P); Vector<BOOL> dp (M +1,false); dp[0] =true;  for(intj =0; J < N; ++j) {if(P[j]! ='*') {                 for(inti =0; I < m; ++i) {dp[i+1] = Dp[i] && (p[j] = = S[i] | | p[j] = ='?'); }            } Else {                inti =0;  while(I < M &&!dp[i]) + +i;  while(I < m) dp[i++] =true; }        }        returnDp[m]; }};

[Leetcode] Wildcard Matching Wildcard Match

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.