Leetcode 10 regular expression matching (正则表达式匹配) (动态规划)

Leetcode 10

问题描述

Given an input string (s) and a pattern (p), 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).

Note:
s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like . or *.

例子

Example 1:
Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:
Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".

Example 3:
Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

Example 4:
Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".

Example 5:
Input:
s = "mississippi"
p = "mis*is*p*."
Output: false

方法

1, If p.charAt(j) == s.charAt(i) :  dp[i][j] = dp[i-1][j-1];
2, If p.charAt(j) == '.' : dp[i][j] = dp[i-1][j-1];
3, If p.charAt(j) == '*': 
   here are two sub conditions:
               1   if p.charAt(j-1) != s.charAt(i) : dp[i][j] = dp[i][j-2]  //in this case, a* only counts as empty
               2   if p.charAt(i-1) == s.charAt(i) or p.charAt(i-1) == '.':
                              dp[i][j] = dp[i-1][j]    //in this case, a* counts as multiple a 
                           or dp[i][j] = dp[i][j-1]   // in this case, a* counts as single a
                           or dp[i][j] = dp[i][j-2]   // in this case, a* counts as empty
** Solution **
** Java **
** 4ms, beats 54.06% **
** 38.5MB, beats 45.45% **
class Solution {
    public boolean isMatch(String s, String p) {
        if (s == null || p == null) 
            return false;

        boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
        dp[0][0] = true;
        for (int i = 1; i < p.length() + 1; i++) 
            if (p.charAt(i - 1) == '*' && dp[0][i - 2]) 
                dp[0][i] = true;
        
        for (int i = 1; i < s.length() + 1; ++i) {
            for (int j = 1; j < p.length() + 1; ++j) {
                if (p.charAt(j - 1) == '.' || p.charAt(j - 1) == s.charAt(i - 1))
                    dp[i][j] = dp[i - 1][j - 1];
                else if (p.charAt(j - 1) == '*') {
                    if (p.charAt(j - 2) != s.charAt(i - 1) && p.charAt(j - 2) != '.')
                        dp[i][j] = dp[i][j - 2];
                    else
                        dp[i][j] = dp[i][j - 2] || dp[i][j - 1] || dp[i - 1][j];
                } 
            }
        }
        return dp[s.length()][p.length()];
    }
}
** Solution Python3 **
** 64ms, beats 42.89% **
** 12.8MB, beats 100.00% **
class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        if (s == None or p == None) :
            return False
        dp = [[False for _ in range(len(p) + 1)] for _ in range(len(s) + 1)]
        dp[0][0] = True
        for i in range(1, len(p) + 1) :
            if (p[i - 1] == '*' and dp[0][i - 2]) :
                dp[0][i] = True
        for i in range(1, len(s) + 1) :
            for j in range(1, len(p) + 1) :
                if (p[j - 1] == "." or p[j - 1] == s[i - 1]) :
                    dp[i][j] = dp[i - 1][j - 1]
                elif (p[j - 1] == '*') :
                    if (p[j - 2] != '.' and p[j - 2] != s[i - 1]) :
                        dp[i][j] = dp[i][j - 2]
                    else :
                        dp[i][j] = (dp[i - 1][j] or dp[i][j - 1] or dp[i][j - 2])
        return dp[-1][-1]
posted @ 2020-02-04 13:18  willwuss  阅读(126)  评论(0编辑  收藏  举报