Leetcode: Regular Expression matching

Source: Internet
Author: User

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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "a*") → trueisMatch("aa", ".*") → trueisMatch("ab", ".*") → trueisMatch("aab", "c*a*b") → true

Difficulty: 100. The situation is too detailed. It is much more difficult than wildcard matching. It is difficult to clarify the ideas in the short time of the interview and refer to the ideas of code ganker.

In this question, assume that we maintain a Boolean array res [I] [J], indicates whether the first I character of S matches the first J character of P (note that the res dimension here is S. length () + 1, p. length () + 1 ). The recurrence formula is similar to the preceding one. There are three situations:
(1) p. charat (j) is not '*'. The situation is relatively simple, as long as you determine if the current s I and P are the same on the J character (if p is on the J character is '. ', also the same), and Res [I] [J] = true, then res [I + 1] [J + 1] is also true; res [I] [J] = false, Res [I + 1] [J + 1] is also false
(2) p. charat (j) is '*', but P. charat (J-1 )! = '.'. If one of the following conditions is met, the value of RES [I + 1] [J + 1] is true:
1) RES [I + 1] [J] is true ('*' only takes the first character once );
2) RES [I + 1] [J-1] is true ('*' is not obtained at a time, that is, ignore these two characters );
3) RES [I] [J + 1] & S. charat (I) = S. charat (I-1) & S. charat (J-1) = P. charat (J-1) (this is equivalent to I from 0 to S. length () is scanned. If the character corresponding to P [J + 1] is '*', it means that the following strings can be matched in sequence. If the following characters are repeated all the time, and the character before ).
(3) p. charat (j) is '*', and P. charat (J-1) = '.'. Because ". * "can match any string, so in the previous res [I + 1] [J-1] Or res [I + 1] [J], as long as I + 1 is true, then the remaining res [I + 1] [J + 1], Res [I + 2] [J + 1],..., res [S. length ()] [J + 1] is true.

This question has a very important point, that is, when the implementation of the outer loop should be P, and then wait until the matching string S's inner loop is scanned.

 1 public boolean isMatch(String s, String p) { 2         if(s.length()==0 && p.length()==0) 3             return true; 4         if(p.length()==0) 5             return false; 6         boolean[][] res = new boolean[s.length()+1][p.length()+1]; 7         res[0][0] = true; 8         for(int j=0;j<p.length();j++)  //Outer circle is p, so scan the column first 9         {10             if(p.charAt(j)==‘*‘)  //if this column of p is ‘*‘11             {12                 if(j>0 && res[0][j-1]) res[0][j+1]=true; //the first row of this column, start fill res[][] from colum 213                 if(j<1) continue; //all elements of column 0 and 1 should be false, except res[0][0], 14                 if(p.charAt(j-1)!=‘.‘) //the case where p.charAt(j)==‘*‘,and p.charAt(j-1)!=‘.‘15                 {16                     for(int i=0;i<s.length();i++)17                     {18                         if(res[i+1][j] || j>0&&res[i+1][j-1] 19                         || i>0 && j>0 && res[i][j+1]&&s.charAt(i)==s.charAt(i-1)&&s.charAt(i-1)==p.charAt(j-1))20                             res[i+1][j+1] = true;21                     }22                 }23                 else   // case where p.charAt(j)==‘*‘ and p.charAt(j-1)==‘.‘ 24                 {25                     int i=0;26                     while(j>0 && i<s.length() && !res[i+1][j-1] && !res[i+1][j])27                         i++;28                     for(;i<s.length();i++)  //since this i, all elements down this column are true29                     {30                         res[i+1][j+1] = true;31                     }32                 }33             }34             else   // case where p.charAt(j) != ‘*‘, general cases, compare s.charAt(i) with p.charAt(j)35             {36                 for(int i=0;i<s.length();i++)37                 {38                     if(s.charAt(i)==p.charAt(j) || p.charAt(j)==‘.‘)39                         res[i+1][j+1] = res[i][j];40                 }41             }42         }43         return res[s.length()][p.length()];44     }

 

leetcode: 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.