[Leetcode questions and Notes] Regular Expression matching

Source: Internet
Author: User

Implement Regular Expression matching with support‘.‘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", ".*") → trueisMatch("aab", "c*a*b") → true

Question: see the recursive solution. This question has been handed in many times, mainly for details. Note the following points:

  • When S is 0, if p is 1, false; otherwise, if the 1st bits of P are '*', we need to evaluate the elements behind P, so we call ismatch (S, p. substring (2). In this example, S = "", P = "C *"; if the first of P is not 1, then S and P certainly do not match.
  • When the length of P is 1, it must be processed separately, because at this time, we do not need to judge whether the 1st bits of P are. The code for processing This section is as follows:
  1. if(p.length() == 1){        if(p.charAt(0) == ‘.‘ && s.length() == 1)            return true;        return s.equals(p);}
  • In other cases, the score is based on whether the 1st bits of P are. If it is not '*', The 0th bits of P and S must match (equal or P 0th bits are '. '), otherwise P and S do not match. In this example, S = "AB", P = "C * B" is similar ". If it is '*', we will match 0 bits, 1 bits, 2 bits ..... similar examples include S = "abbc", P = "AB * BBC". At this time, '*' does not match any character in S, then S = "AA", P = "A *", '*' matches two a s.

The Code is as follows:

 1 public class Solution { 2     public boolean isMatch(String s, String p) { 3          if(p.length() == 0) 4             return s.length() == 0; 5          6         if(s.length() == 0){ 7             if(p.length() == 1) 8                 return false; 9             if(p.charAt(1) == ‘*‘)10                 return isMatch(s, p.substring(2));11             return false;12         }13         14         15         if(p.length() == 1){16             if(p.charAt(0) == ‘.‘ && s.length() == 1)17                 return true;18             return s.equals(p);19         }20             21         if(p.length() >= 2 && p.charAt(1) != ‘*‘){22             //if p(1) is not *, we need p(0) equals to s(0) or p(0) equals ‘.‘23             if(p.charAt(0) == s.charAt(0) || p.charAt(0) == ‘.‘ && s.length() != 0)24                 //check if the left is also match25                 return isMatch(s.substring(1), p.substring(1));26             return false;27         }28         else{29             //if p(1) is ‘*‘,we check how many we can match from this * by trying30             int i = 0;31             char now = p.charAt(0);32             while(i<s.length() && (s.charAt(i) == now || now == ‘.‘)){33                 if(isMatch(s.substring(i+1),p.substring(2)))34                         return true;35                 i++;36             }37             //this means we don‘t use this ‘*‘ to match any character, just skip it38             return isMatch(s, p.substring(2));39         }40     }41 }

 

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.