Regular Expression Matching

Source: Internet
Author: User

https://leetcode.com/problems/regular-expression-matching/

Implement regular expression matching with support for ‘.‘ and ‘*‘ .

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

Problem Solving Ideas:

Well, this is one of the hardest topics to do leetcode, and I admit I'm not. Can only see other people's solution, summarized as follows.

The difficulty of the subject is mainly on the * in P, because if the current character of P is a letter, it can be directly matched with S, judge whether to continue, if it is '. ', also can match any character of S, judge whether to continue. But if P is currently *, it depends on how many of the previous characters of P can represent in S. In fact, it's hard to have a way to determine that it can only be enumerated, possibly one, 2, or 3. This question is given in the first example of the last reference URL.

So the idea of the subject can be broadly divided into two chunks.

1. The next character of P is not *. At this point we can boldly determine if the current character of S and P matches (this contains the current p as '. ') of the situation). If the match, then S and p each advance one character, continue to judge, if does not match, can return false directly.

2. The next character of P is *.

2.1 s and P's current characters do not match, S does not move, p skips this character and next ' * ', continues to judge. Just like the last example in the title.

2.2 s and P's current character match (this contains P currently '. ') ), assuming that ' x ' is the case. We're going to look at P's next * exactly how many ' x ' are represented in s? As a result, s moves backwards by step 1, judging if p skips this character and the next ' * ' part, and s the back part matches. As long as there is a likelihood of matching, you can return true.

Of course, here s the range to move backward must be within the contiguous n ' x ', or P or '. '.

 Public classSolution { Public BooleanIsMatch (String s, String p) {returnIsmatchhelper (S, p, 0, 0); }         Public BooleanIsmatchhelper (string S, String p,intIintj) {//P has ended, and s also ends, returns true        if(J >=p.length ()) {            returnI >=s.length (); }        //p to the last character, s must also be the last character, and the two are equal (at this point the P cannot be *, because * is always skipped along with its previous character)        if(j = = P.length ()-1) {            return(i = = S.length ()-1) && (S.charat (i) = = P.charat (j) | | P.charat (j) = = '. '); }        //1. If the next character of P is not *, if the current character of S and P can match, continue down, the mismatch returns false directly        if(P.charat (j + 1)! = ' * ') {            //But at this point S has finished matching, return false            if(i = =s.length ()) {                return false; }            if(P.charat (j) = = '. ') {                returnIsmatchhelper (S, p, i + 1, j + 1); }            returnS.charat (i) = = P.charat (j) && Ismatchhelper (S, p, i + 1, j + 1); }        //2. If the next character of P is *, i.e. P.charat (j + 1) = = ' * '//2.1 If the current character of S and P is equal, or P is currently.        if(I < S.length () && (S.charat (i) = = P.charat (j) | | P.charat (j) = = '. ')) {            //on the premise of S.charat (i) = = P.charat (j), try S to fetch 1, 2, 3 ... The same character, look at the back as long as matching a situation can be             while(I < S.length () && (S.charat (i) = = P.charat (j) | | P.charat (j) = = '. ')) {                if(Ismatchhelper (S, p, I, J + 2)) {                    return true; } I++; }        }        //the next character of the 2.2 p is *, and is not currently., S and P are not equal to the current character, just skip the two characters of p to compare backwards, such as the last example of the topic//if (S.charat (i)! = P.charat (j) && P.charat (j)! = '. ') {            returnIsmatchhelper (S, p, I, J + 2); // }    }}

Http://articles.leetcode.com/2011/09/regular-expression-matching.html

http://blog.csdn.net/fightforyourdream/article/details/17717873

http://blog.csdn.net/linhuanmars/article/details/21145563

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.