Regular Expression Matching & Wildcard Matching

Source: Internet
Author: User

Regular Expression Matching

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)
  Example
IsMatch ("AA", "a") →falseismatch ("AA", "AA") →trueismatch ("AAA", "AA") →falseismatch ("AA", "A *") →trueismatch ("AA", ". *") ") →trueismatch (" AB ",". * ") →trueismatch (" AaB "," C*a*b ") →true

Analysis: If I did not review the interview when the problem, and then the interviewer to test the problem, I will be in the letter to the interviewer wrote two words: Fuck you! This guy obviously doesn't want me to. It took me more than one hours to understand the meaning of the question. 1.' * ' Matches zero or more of the preceding element.  It doesn't mean that for a *, it can only match a, AA, AAA, infinitely multiple A. It can also match "" (an empty string). Lie down, do you want to get it, do you want it, do you really want it? You can't imagine it! 2. ". *" means that you can match any string, such as "ddd", "DDA", "abc". 3. For "A*b", it matches "B", but does not match "a". 4. If p starts with "*", you have to get rid of it. Well, if you understand the above section, we can solve the problem with DP. We first create a two-dimensional array to hold the intermediate variables.

int m = P.length ();
int n = s.length ();
boolean[][] Match = new Boolean[m + 1][n + 1]; (p is horizontal axis, S is longitudinal)

MATCH[I][J] Indicates whether the first i-1 characters of P match the first j-1 characters of S.

Here are a few things:

If P.chartat (i) is "." or P.charat (i-1) = = S.charat (j-1), then we have:

MATCH[I][J] = match[i-1][j-1];

If P.chartat (i) is not "." and P.charat (i-1)! = S.charat (j-1), then we have:

MATCH[I][J] = false;

OK, the key point is, if p.chartat (i) = = ' * ', then how to do?

First, if P.charat (i-2) = = '. ' | | P.charat (i-2) = = S.charat (j-1)

Then we can not take match[i-1][j-1] (because P.charat (i-1) = = S.charat (j-1) If the above conditions are established), or Match[i-2][j] ("x*" directly into "), or Match[i ][j-1] ("x*" becomes "x*x") | | MATCH[I-1][J] ("x*" becomes "X");

So, we have: match[i][j] = match[i-1][j-1] | | MATCH[I-2][J] | | Match[i][j-1] | | MATCH[I-1][J];

If P.charat (i-2)! = S.charat (j-1), we have only one way:

MATCH[I][J] = Match[i-2][j];

1  Public classSolution {2     /**3 * @param s:a string4 * @param p:a string includes "." and "*"5 * @return: A boolean6      */7      PublicBoolean IsMatch (string s, String p) {8         if(s = =NULL|| p = =NULL)return false;9         Ten          while(P.length () >=1&& P.charat (0) =='*') { Onep = p.substring (1); A         } -         intm =p.length (); -         intn =s.length (); theboolean[][] Match =NewBoolean[m +1][n +1]; -match[0][0] =true; -          for(inti =1; I <= m; i++) { -             if(P.charat (I-1) =='*') { +match[i][0] = Match[i-2][0]; -             } +         } A          at          for(inti =1; I <= m; i++) { -              for(intj =1; J <= N; J + +) { -                 if(P.charat (I-1) = = S.charat (J-1) || P.charat (I-1) =='.') { -MATCH[I][J] = match[i-1][j-1];  -}Else if(P.charat (I-1) =='*') { -                     if(P.charat (I-2) =='.'|| P.charat (I-2) = = S.charat (J-1)) { inMATCH[I][J] = match[i-1][j-1] || Match[i-2][J] | | Match[i][j-1] || Match[i-1][j]; -}Else { toMATCH[I][J] = match[i-2][j]; +                     } -}Else { theMATCH[I][J] =false; *                 } $             }Panax Notoginseng         } -         returnMatch[m][n]; the     } +      A}
Wildcard Matching

Implement wildcard pattern matching with the support for and ‘?‘ ‘*‘ .

    • ‘?‘Matches any single character.
    • ‘*‘Matches any sequence of characters (including the empty sequence).

The matching should cover the entire input string (not partial).

Has you met this question in a real interview?Yes Example
isMatch("aa","a") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → false
1  Public classSolution {2     /**3 * @param s:a string4 * @param p:a string includes "?" and "*"5 * @return: A boolean6      */7      PublicBoolean IsMatch (string s, String p) {8         if(s = =NULL|| p = =NULL)9             return false;Tenboolean[][] Match =NewBoolean[p.length () +1][s.length () +1]; Onematch[0][0] =true; A          for(inti =1; I < match[0].length; i++) { -match[0][i] =false; -         } the          -          for(inti =1; i < match.length; i++) { -             if(P.charat (I-1) =='*') { -match[i][0] = Match[i-1][0]; +             }  -         } +          A          for(inti =1; i < match.length; i++) { at              for(intj =1; J < match[0].length; J + +) { -                 if(P.charat (I-1) = = S.charat (J-1) || P.charat (I-1) =='?') { -MATCH[I][J] = match[i-1][j-1]; -}Else if(P.charat (I-1) =='*') { -MATCH[I][J] = match[i-1][j-1] || Match[i-1][J] | | Match[i][j-1]; -                 } in             } -         } to          +         returnmatch[p.length ()][s.length ()]; -     } the}

Regular Expression Matching & Wildcard 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.