Leetcode [10] (Java): Regular Expression Matching

Source: Internet
Author: User

title : matching Regular expressions

problem Difficulty: Hard

title Content : 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)

translation :

"  . "matches any one character.

"*" matches 0 or more of the preceding element

The match should include the entire input string (not part).

Some examples:ismatch ("AA", "a") →falseismatch ("AA", "AA") →trueismatch ("AAA", "AA") →falseismatch ("AA", "A *") → Trueismatch ("AA", ". *") →trueismatch ("AB", ". *") →trueismatch ("AaB", "C*a*b") →true

my train of thought : Oh da, no train of thought ...

Answer :

1      Public BooleanIsMatch (string text, string pattern) {2         if(Pattern.isempty ())returntext.isempty ();3         BooleanFirst_match = (!text.isempty () &&4(Pattern.charat (0) = = Text.charat (0) | | Pattern.charat (0) = = '. '));5         6         if(Pattern.length () >= 2 && pattern.charat (1) = = ' * '){7             return(IsMatch (Text, pattern.substring (2)) | |8(First_match && IsMatch (text.substring (1, pattern));9}Else {Ten             returnFirst_match && IsMatch (text.substring (1), pattern.substring (1)); One         } A}

Answer Ideas :

1, each element of the step backwards comparison, each level (element) will have a result, and there are branch judgments , if the use of loops to make judgments too cumbersome, so you can consider recursion ;

2, when there are multiple parameters joint judgment, the first list to write judgment : (0 is empty)

Text pattern

0 0 True

0 1 First = flase " There may be a case where the pattern is x*, and the text has been chopped to empty, and the result may be a match, and this situation should go into the subsequent judgment"

1 0 False

1 1 First = comparison

The criteria for the award are written according to this form.

3. The current comparison result is a letter comparison or the current pattern is ". ”

4. The following comparisons are divided into two cases:

A, the first two of the pattern is x*, and the result of this condition is two kinds of results or

     I,the pattern of the x* has been matched with the corresponding characters in text, the next pattern needs to be matched, so the pattern of x* removed

     II,the pattern of the x* with the text of the current letter match completed, the next text needs to be matched, so the current results and text minus one of the results of the combination

b, other conditions directly to the text and pattern each cut one, with the current comparison results together to return.

Q: Why does the current result not be returned directly?

A: There may be a case where the pattern is x*, and the text has been chopped to empty, which may result in a match. Only if the pattern is empty does not match to return directly.

Leetcode [10] (Java): 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.