Regular Expression Matching

Source: Internet
Author: User

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

The basic idea is to see if the string s and P's substring from I and J are matched, and the recursive method is used until the end of the string, and finally back to get the result. Assuming that we now go to the I position of S, P's J position, the situation is divided into the following two kinds:
(1) p[j+1] not ' * '. The situation is relatively simple, as long as the current S I and P J characters are the same (if there is a P on J character is '. ', is the same), if different, return false, otherwise, recursive next layer i+1,j+1;
(2) p[j+1] is ' * '. Then look at the substring starting from s[i], assuming that s[i],s[i+1],... s[i+k] are equal to p[j] then it means that these are likely to be suitable matches, then recursion for the remainder (i,j+2), (i+1,j+2),..., (i+k,j+2) to try (j +2 is due to skipping the current and next ' * ' characters).
The C + + implementation code is as follows:

#include <iostream>#include<string>using namespacestd;classSolution { Public:    BOOLIsMatch (Const Char*s,Const Char*p) {returnHelperstring(s),string(p),0,0); }    BOOLHelperConst stringSConst stringp,size_t i,size_t J) {        if(j==p.length ())returni==s.length (); //P.charat (j+1)! = ' * '        if(J==p.length ()-1|| p[j+1]!='*')        {            if(I==s.length () | | (s[i]!=p[j]&&p[j]!='.'))                return false; Else                returnHelper (s,p,i+1, j+1); }        //P.charat (j+1) = = ' * '         while(I<s.length () && (s[i]==p[j]| | p[j]=='.'))        {            if(Helper (s,p,i,j+2))                return true; I++; }        returnHelper (s,p,i,j+2); }};intMain () {solution SS; Const Char*s="AA"; Const Char*p=". *BBB"; cout<<ss.ismatch (s,p) <<Endl;}

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.