"Leetcode-Interview algorithm classic-java Implementation" "010-regular expresssion Matching (regular expression matching)"

Source: Internet
Author: User

"010-regular expresssion Matching (regular expression matching)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

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") →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

Main Topic

Implements a regular expression matching algorithm that matches any one character, * matches 0 or more leading characters

Thinking of solving problems

Use the tag-matching algorithm to match from back to forward.

Code Implementation
ImportJava.util.Arrays; Public  class solution {    /** * 010-regular expresssion Matching (regular expression match) * * @param s Match String * @param P-mode string * @return match result, true match, false mismatch */     Public Boolean IsMatch(string S, String p) {//array of markers        Boolean[] match =New Boolean[S.length () +1];//InitializeArrays.fill (Match,false);//Assume that the final result is a matchMatch[s.length ()] =true;//The pattern string is processed from the back forward         for(inti = P.length ()-1; I >=0; i--) {//If current is *            if(P.charat (i) = =' * ') {//Match string to start processing from the last                 for(intj = s.length ()-1; J >=0; j--) {Match[j] = Match[j] | | match[j +1] && (P.charat (i-1) =='. '|| S.charat (j) = = P.charat (i-1));            } i--; }//If not *            Else{ for(intj =0; J < S.length (); J + +) {Match[j] = match[j +1] && (P.charat (i) = ='. '||                P.charat (i) = = S.charat (j)); } match[s.length ()] =false; }        }returnmatch[0]; }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/46951847"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "010-regular expresssion Matching (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.