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]

原文地址:https://www.cnblogs.com/willwuss/p/12259119.html

时间: 2024-10-22 14:25:34

Leetcode 10 regular expression matching (正则表达式匹配) (动态规划)的相关文章

[LeetCode]10. Regular Expression Matching正则表达式匹配

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 (no

(待解决)LeetCode 10. Regular Expression Matching 解题报告

10. Regular Expression Matching 提交网址: https://leetcode.com/problems/regular-expression-matching/ Total Accepted: 79548 Total Submissions: 361279 Difficulty: Hard Implement regular expression matching with support for '.' and '*'. '.' Matches any sing

LeetCode 10 Regular Expression Matching (正则表达式匹配)

翻译 实现支持"."和"*"的正则表达式匹配. "." 匹配支持单个字符 "*" 匹配零个或多个前面的元素 匹配应该覆盖到整个输入的字符串(而不是局部的). 该函数的原型应该是: bool isMatch(const char * s, const char * p) 示例: isMatch("aa","a") → false isMatch("aa","a

[LeetCode] 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

LeetCode (10): Regular Expression Matching [HARD]

https://leetcode.com/problems/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 ent

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

Java [leetcode 10] Regular Expression Matching

问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. http://i.cnblogs.com/EditPosts.aspx?opt=1 The matching should cover the entire input string

[LeetCode] 10. 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

LeetCode 10 Regular Expression Matching (C,C++,Java,Python)

Problem: 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