[Eetcode 10] Regular Expression Matching

Source: Internet
Author: User
Tags function prototype

1 Topics:

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

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

2 Ideas:

OK, this question I started a comparison really kneeling, no recursion, consider various situations, various if-else, found still can't consider all the situation. See what others write, use recursion to write, the idea is very clear.

Https://leetcode.com/discuss/32424/clean-java-solution

Look at that C + +, want to turn Java, really kneeling, C + + with ' \ ' to represent the end of the string, and Java string is an array, without this one said, various cross-border plus timeout.

Https://leetcode.com/discuss/9405/the-shortest-ac-code

3 Code:

     Public BooleanIsMatch (String s, String p) {if(P.isempty ()) {returnS.isempty (); }        if(p.length () = = 1 | | P.charat (1)! = ' * ') {            if(S.isempty () | | (P.charat (0)! = '. ' && p.charat (0)! = S.charat (0))) {                return false; } Else {                returnIsMatch (s.substring (1), p.substring (1)); }        }        //p.length () >=2 && p.charat (1) = = ' * '//The first char is match         while(!s.isempty () && (S.charat (0) = = P.charat (0) | | P.charat (0) = = '. ')) {              if(IsMatch (S, p.substring (2))) {                 return true; }            //See if the next char of S is still matchs = s.substring (1); }        //First char not match, make * to 0        returnIsMatch (S, p.substring (2)); }



[Eetcode 10] 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.