Leetcode--regular Expression Matching

Source: Internet
Author: User

‘.‘ 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 (const char *s, const char *p) Some examples:ismatch ("AA", "a") →falseismatch ( "AA", "AA") →trueismatch ("AAA", "AA") →falseismatch ("AA", "A *") →trueismatch ("AA", ". *") →trueismatch ("AB", ". *") →true IsMatch ("AaB", "C*a*b") →true

The original question above

Leetdcode to the title of the mark is a DP, although DP can do, but I really do not think that DP is a very intuitive method. Well, I'm a normal young man, the hardest part of the problem is backtracking, backtracking, but I'm stuck here.

For a match, a very intuitive approach is to greedy, as much as possible to match more characters. However, because * this character may match 0, 1 or more, resulting in exactly how many times the greedy algorithm is not very well controlled. What if the "*" Match puzzle is resolved?

We still match the characters from beginning to end, and for the processing of ' * ' We are as follows:

    1. If the next character of P is not ' * ' and then it must match the current character of s. Continue pattern matching with the next character of both s and p.

    2. If the next character of P are ' * ', then we do a brute force exhaustive matching of 0, 1, or more repeats of Curre NT character of p... Until we could not match any more characters.

If the next character is ' * ', then we should match 0 times, 1 times, do a brute force solution several times, and consider all the circumstances. In each case, it is solved in a recursive way.

Since it is recursive, it is necessary to consider base well. I think this recursive base should be a match string and the matching string has all been scanned.

Int ismatch (char* s, char* p) {    assert (s && p);  //s and p are null    if  (*p ==  ')      {        return *s ==  ';   '   }    if  (* (p+1)  !=  ' * ')   //next character  is not  ' * '     {        assert (*p !=   ' * ');        if  ((*p == *s)  | |   (*p ==  '. ')  && *s !=  '))         {             return ismatch (s+1, p+1);         }else{             return&nbsP;0;        }    }else{ //next character  is  ' * ',  match 0, 1, 2,...  times         while  ((*p == *s)  | |   (*p ==  '. ')  && *s !=  '))         {             if  (IsMatch (s, p+2))              {                 return 1;             }            s++;         }        return ismatch (s),  p+2);     }}


Leetcode--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.