Leetcode:regular Expression Matching

Source: Internet
Author: User
Tags function prototype

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
Analysis: The problem can still be categorized as a detailed problem, although seemingly complex, but careful analysis found that the logic, or relatively easy to achieve. So encounter more complex problems, to carefully analyze the possible situation, and then classify the different situations, so as to achieve from complex to simple.
We first discuss the characters following the current first character in P. Assuming P points to the current character, then there are two possible characters for p+1, one is ' * ' and the other is not ' * ' (feeling like crap).
1. If p+1 is not ' * ', if *p = = *s or *p = = '. ' and *s! = ' \ ', then p+1, two strings after s+1 may match; otherwise, two strings do not match.
2. If the p+1 is ' * '. Here we can be divided into three kinds of situation discussion:
1) if *p! = *s, then we need to judge IsMatch (S, p+2), because ' * ' here means the number of characters p points to is 0.
2) If *p = = *s, then we need to consider ' * ' to represent one or more *p cases.
3) If *p = = '. ' && *s! = ', then we need to consider the case that ' * ' represents a character to ' * ' for all characters after *s.
With the above analysis, we can implement IsMatch in a recursive way, the code is as follows:
classSolution { Public:    BOOLIsMatch (Const Char*s,Const Char*p) {if(*p = =' /')return(sb==' /'; if(* (p+1) !='*'){//If Next is not ' * '            if(*p = = *s | | *p = ='.'&& *s! =' /')                returnIsMatch (s+1, p+1); Else return false; }Else{             while(*p = = *s | | *p = ='.'&& *s! =' /'){                if(IsMatch (s,p+2))                    return true; S++; }            returnIsMatch (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.