#10 Regular Expression Match

Source: Internet
Author: User

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


Implement regular expression matching with 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", "A *") →trueismatch ("AA", ". *") →trueismatch ("AB", ". *") →true IsMatch ("AaB", "C*a*b") →true


BOOL IsMatch (char* S, char* p) {    if (*p = = ' && ' *s ')        return true;    if (*p && * (p + 1) = = ' * ') {       //if P is followed by *, the matching failure requires backtracking if        (*p! = '. ' && *s! = *p | | *s = = ' + ')   //If the current word Characters do not match, * can only take 0, remove the first two characters of P and then recursively match            return IsMatch (S, p + 2);        if (IsMatch (s, p + 2))   //fetch * is 0 times, if can match, return match succeeded;            return true;        Return IsMatch (S + 1, p);   Otherwise backtracking, remove the s initials (* at least once) to continue the recursive match                }    else if (*p = = '. ' && *s! = ') ') | | *p = = *s)      //current character matching, recursive comparison sequence; C15/>return IsMatch (S + 1, p + 1);    else        return false;} Note '. ' does not match all characters, because it is possible to '/';


#10 Regular Expression 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.