Leetcode Regular Expression Matching

Source: Internet
Author: User

The original title link is here: https://leetcode.com/problems/regular-expression-matching/

Regular expressions It is very unrealistic to expect a character-by-character match to be expected. And the "match" issue is very easy to convert to "match a part" and the entire match does not match.

So look for "the rest of the match" situation. It's a good thing to turn a big problem into a smaller problem: recursion

If pattern is a "x*" type, then the pattern is reduced by two two. Otherwise, it is a reduction of one. No matter how you reduce it, make sure there are so many pattern. For example, S.substring (n), where n is the largest and s.length ()

The terminating condition of the recursion is no? The length of P is 0 to see if s is 0, or P is 1 to see the last character of S and P RP the last char on or P is '. '.

Time Complexity:o (2^n).

AC Java:

1  Public classSolution {2      Public BooleanIsMatch (String s, String p) {3         if(p.length () = = 0){4             returnS.length () = = 0;5         }6         if(p.length () = = 1){7             returnS.length () = = 1 && (s.charat (0) = = P.charat (0) | | P.charat (0) = = '. ');8         }9         Ten         //after the above two if, the length of P is already greater than or equal to 2 One         if(P.charat (1)! = ' * ') {//The second character of P is not ' * ' A             returnS.length () >0 -&& (S.charat (0) = = P.charat (0) | | P.charat (0) = = '. ')//whether the first character is equal or the first character of the pattern is '. ' -&& IsMatch (s.substring (1), p.substring (1));//whether the remaining strings match the}Else{//P The second character is ' * ' -             if(IsMatch (S, p.substring (2))){ -                 return true; -             } +             returnS.length () >0//string length needs to be greater than 0 -&& (S.charat (0) = = P.charat (0) | | P.charat (0) = = '. ')//the current string is equal or the first character of a pattern is '. ' +&& IsMatch (s.substring (1), p);//Remove the first character of S A         } at     } -}

Ref:http://harrifeng.github.io/algo/leetcode/regular-expression-matching.html

Method two is to use DP, in fact, is a train of thought.

DP[I][J] Represents the length of I s and the length of J P can match, and finally return Dp[s.length ()][p.length ()].

So the DP generation should be set to a size of new Boolean[s.length () +1][p.length () +1].

AC Java:

1  Public classSolution {2      Public BooleanIsMatch (String s, String p) {3         //Method 2 DP4         Boolean[] DP =New Boolean[S.length () +1] [P.length () +1];5Dp[0][0] =true;6          for(inti = 0; I<=s.length (); i++){7              for(intj = 1; J<=p.length (); J + +){8                 if(i!=0){9                     Charc = P.charat (j-1);Ten                     if(j>1 && c = = ' * ') {//the P-tail character is ' * ' OneDP[I][J] = dp[i][j-2]//' * ' Match 0 A|| DP[I][J-1]//' * ' match 1 -|| (Issame (S.charat (i-1), P.charat (j-2)) && dp[i-1][j]);//' * ' Match multiple -}Else{//P-tail character is not ' * ' the                         if(Issame (S.charat (i-1), c) && dp[i-1][j-1]){ -DP[I][J] =true; -                         } -                     } +}Else{//S is an empty string -                     if(j>=2 && p.charat (j-1) = = ' * ' && dp[0][j-2]){ +DP[0][J] =true; A                     } at                 } -             } -         } -         returndp[s.length ()][p.length ()]; -     } -     Private BooleanIssame (CharSCharp) { in         returnp = = '. ' | | p = =s; -     } to}

Ref:http://www.cnblogs.com/icecreamdeqinw/p/4317183.html

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.