leetcode.010 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

Test instructions
With wildcard characters with string matching.
.        匹配任意的单个字符 *        匹配前一个字符的零个或者多个出现Ideas draw on the beauty of code in the solution, using recursive methods, code concise, easy to understand.
1 //excerpt from the beauty of code2 //character meaning3 // . Match any single character4 //^ matches the beginning of the input string5 //$ matches the end of the input string6 //* Match 0 or more occurrences of the previous character7#include <stdio.h>8 intMatchhere (Char*regexp,Char*text);9 Ten intMatchstar (intCChar*regexp,Char*text) {//Matchstar:search for C*regexp at beginning of text One     Do{//A * matches zero or more instances A        if(Matchhere (regexp, text))return 1; -} while(*text! =' /'&& (*text++ = = c | | c = ='.')); -    return 0; the } - intMatchhere (Char*regexp,Char*text) {//Matchhere:search for RegExp at beginning of text -    if(regexp[0] ==' /')return 1; -    if(regexp[1] =='*')returnMatchstar (regexp[0], regexp+2, text); +    if(regexp[0] =='$'&& regexp[1] ==' /')return*text = =' /'; -    if(*text!=' /'&& (regexp[0]=='.'|| regexp[0]==*text))returnMatchhere (regexp+1, text+1); +    return 0; A } at  - intMatchChar*regexp,Char*text) {//Match:search for regexp anywhere in text -     if(regexp[0] =='^')returnMatchhere (regexp+1, text); -      Do{//must look even if string is empty -         if(Matchhere (regexp, text))return 1; -} while(*text++! =' /'); in     return 0; -}
View Code

Here is your own code for this topic.

1 BOOLIsMatch (Char* S,Char*p);2 BOOLMatchstar (intCChar*s,Char*p)3 {4      Do{//A * matches zero or more instances5         if(IsMatch (S, p))return 1;6} while(*s! =' /'&& (*s++ = = c | | c = ='.'));7     return 0;8 }9 BOOLIsMatch (Char* S,Char*p) {Ten     if(p[0]==' /'&& s[0]==' /')return 1; One     if(p[1]=='*')returnMatchstar (p[0], s,p+2); A     if(*s!=' /'&& (p[0]=='.'|| *s==p[0]))returnIsMatch (s+1, p+1); -     return 0; -      the}

leetcode.010 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.