Question 10 of leetcode -- Regular Expression matching

Source: Internet
Author: User

Problem: implement regular expression matching with support '. 'and '*'. '. 'matches any single character. '*' matches zero or more of the preceding element. the matching shocould cover the entire input string (not partial ). the function prototype shoshould be: bool ismatch (const char * s, const char * P) some examples: ismatch ("AA", "A") → false ismatch ("AA ", "AA") → true ismatch ("AAA", "AA") → false ismatch ("AA", "A *") → true ismatch ("AA ",". * ") → true ismatch (" AB ",". * ") → true ismatch (" AAB "," C * a * B ") → true

It is a regular expression. You can refer to Baidu to learn the regular expression first. * Indicates that the first character appears zero or multiple times. For example, Zo * can match Z because * indicates that the preceding character o appears zero times, so it matches Z. Similarly, Zo * can also match Zo, zoo, zoooo, zooooooo, and so on. The dot '.' Is a wildcard that matches any single character. Then, the star '. *' can match any number of characters, because * indicates any number of duplicates in the front. So it is the repetition of multiple '.', and '.' is an arbitrary match, so any combination can be used. The first point matches a. The second point does not only match a, but can also match any character. So '. *' is omnipotent. I should have not understood the error. After a long time, I did not think of a good idea and had to use recursion.

class Solution {public:bool isMatch(const char *s, const char *p){    if(s == NULL || p == NULL)        return false;    if (*p == ‘\0‘)        return *s == ‘\0‘;    if (*(p + 1) == ‘*‘)    {        while(*s == *p || *p == ‘.‘ && *s != ‘\0‘)        {            if (isMatch(s, p + 2))                return true;            s++;        }        return isMatch(s, p + 2);    }    else if (*s == *p || *p == ‘.‘&&*s!=‘\0‘)        return isMatch(s + 1, p + 1);    return false;}};

Submit multiple attempts to put if (* P = '\ 0 ')Return * s = '\ 0 ';Ignore. Because it is recursive, it cannot be left blank.

Question 10 of 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.