[Leetcode] 010. Regular Expression Matching (Hard) (C++/java/python)

Source: Internet
Author: User

Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode


010.regular_expression_matching (Hard)
links

Title: https://oj.leetcode.com/problems/regular-expression-matching/
Code (GitHub): Https://github.com/illuz/leetcode

Test Instructions

Give an original string and a regular expression, and ask if you can match.

Analysis
    1. The lazy way is directly with the language of the regular implementation. (Python is also a sentence =w=)
    2. Using the DFS method
    3. You can use the DP method
      • With array DP: For dp[i][j] s[0..i] and P[0..J] whether match, when p[j] != ‘*‘ , b[i + 1][j + 1] = b[i][j] && s[i] == p[j] , when p[j] == ‘*‘ you want to classify the discussion, the specific reference to DP C + +, you can also compress the DP into one dimension: refer to Here
      • Using memory, is to save the results of the calculation, the next time you don't have to forget
Code

C + +: (DFS)

Class Solution {public:    bool IsMatch (const char *s, const char *p) {if (!p[0]) return!s[0];int Slen = strlen (s), Plen  = strlen (P); if (Plen = = 1 | | p[1]! = ' * ') return Slen && (p[0] = = '. ' | | s[0] = p[0]) && IsMatch (S + 1, p + 1) while (S[0] && (p[0] = = '. ' | | s[0] = = p[0])) if (IsMatch (s++, p + 2)) return True;return IsMatch (s, p + 2);}};


Java: (Array DP)

public class Solution {public Boolean IsMatch (string s, string p) {int lens = s.length ();        int LENP = P.length ();        if (lens = = 0 && LENP = = 0) return true;        Init boolean[][] dp = new BOOLEAN[2][LENP + 1];        Dp[0][0] = dp[1][0] = true; for (int j = 2; J <= Lenp; ++j) {if (P.charat (j-1) = = ' * ' && dp[0][j-2]) {dp[0]            [j] = dp[1][j] = true;            }}//dp for (int i = 1; I <= lens; ++i) {dp[i&1][0] = false; for (int j = 1; j <= Lenp; ++j) {Dp[i&1][j] = ((P.charat (j-1) = = S.charat (i-1) | | P.charat (J- 1) = = '. ') && dp[1-(i&1)][j-1]) | | P.charat (j-1) = = ' * ' && (P.charat (j-2) = = S.charat (i-1) | | P.charat (j-2) = = '. ') && dp[1-(i&1) ][J] | | (J >= 2 && p.charat (j-1) = = ' * ' && dp[i&1][j-2]);           }} return dp[lens&1][lenp]; }}


Python: (Memory DP)

Class Solution:    cache = {}    # @return A Boolean    def isMatch (self, S, p):        if (S, p) in Self.cache:            retur N self.cache[(S, p)]        if not p:            return not S        if Len (p) = = 1 or p[1]! = ' * ':            self.cache[(s[1:], p[1:])] = SE Lf.ismatch (s[1:], p[1:])            return len (s) > 0 and (p[0] = = '. ' or s[0] = = p[0])                 and self.cache[(s[1:], p[1:])]
   while s and (p[0] = = '. ' or s[0] = = p[0]): self.cache[(S,            p[2:])] = Self.ismatch (S, p[2:])            if self.cache[(s, P[2:])]:                return True            s = s[1:]        self.cache[(S, p[2:])] = Self.ismatch (S, p[2:])        return self.cache[( S, p[2:])]


Python: (with regex)

Class solution:    # @return A Boolean    def isMatch (self, S, p):        return Re.match (' ^ ' + p + ' $ ', s)! = None



[Leetcode] 010. Regular Expression Matching (Hard) (C++/java/python)

Related Article

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.