Leetcode Notes Regular Expression Matching

Source: Internet
Author: User

First come to the topic:

‘.‘ 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

HMM about this problem at the beginning of the first to see the question to me the general idea is easy to come out. Then there is the approximate framework.

The main thing is to divide the situation clearly. About '. ' And the use of ' * ' and several special cases. In the beginning, I was completely unaware that I could use IMatch, and then I was thinking how to match.

The answer is for reference only. 0.0 The main logical problem to distinguish the case is the most important

public class Solution {public Boolean IsMatch (string s, String p) {//easy to think about if (p.length () =        =0) {return s.length () ==0;            }//special Case if (p.length () ==1) {if (s.length () <1) {return false; } else if ((S.charat (0)!=p.charat (0)) && (P.charat (0)! = '. '))            {return false;            } else{return IsMatch (s.substring (1), p.substring (1));                }}//easy to write, hard to think if (P.charat (1)! = ' * ') {if (s.length () <1) {            return false; } else if ((P.charat (0)!=s.charat (0)) && (P.charat (0)! = '. '))            {return false;            }else{return IsMatch (s.substring (1), p.substring (1));            }}//most Difficult one else{if (IsMatch (s,p.substring (2))) {return true; } inti=0; while (I<s.length () && (S.charat (i) ==p.charat (0) | | P.charat (0) = = '. '))                {if (IsMatch (s.substring (i+1), p.substring (2))} {return true;            } i++;        } return false; }                            }}

Leetcode Notes 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.