Regular Expression Matching

Source: Internet
Author: User

Implement Regular expression matching ‘.‘ with support for and ‘*‘ .

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

Determines whether regular expressions are valid and regular expressions are used. To represent any character with a * to denote one or more previous characters according to these two points in the judgment of the most important thing is to distinguish between the existence or not

The code is as follows:

public class Solution {public    Boolean isMatch (string s, String p) {        if (p.length () ==0) return s.length () ==0;if (p. Length () ==1) {if (s.length () <1| | S.charat (0)!=p.charat (0) &&p.charat (0)! = '. ') Return False;else{return IsMatch (s.substring (1), p.substring (1));}} if (P.charat (1) = = ' * ') {if (IsMatch (s,p.substring (2))) return true;for (int i=0;i<s.length (); i++) {    if (P.charat ( 0)! = S.charat (i) &&p.charat (0)! = '. ') Return False;if (IsMatch (s.substring (i+1), p.substring (2)) return true;}} Else{if (s.length () <1| | (P.charat (0)! = '. ' &&s.charat (0)!=p.charat (0)) return False;else{return IsMatch (s.substring (1), p.substring (1));}} return false;    }}


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.