10. Regular Expression Matching

Posted antoniosu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 (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

难度系数

Hard

解法:动态规划

其中dp[i] [j]表示s[0,i)和p[0,j)是否match,注意左闭右开,状态转移方程如下

1.  P[i][j] = P[i - 1][j - 1], if p[j - 1] != ‘*‘ && (s[i - 1] == p[j - 1] || p[j - 1] == ‘.‘);p和s当前位置字符相等或者p当前位置是‘.‘
2.  P[i][j] = P[i][j - 2], if p[j - 1] == ‘*‘ 匹配0次,匹配0次:s=ab,p=aba*,将p的最后一个a去掉。
3.  P[i][j] = P[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == ‘.‘), if p[j - 1] == ‘*‘ 匹配至少一次

具体代码如下

class Solution {
public:
    bool isMatch(string s, string p){
        int m = s.length(),n = p.length();
        //其中dp[i][j]表示s[0,i)和p[0,j)是否match
        bool dp[m+1][n+1];
?
        dp[0][0] = true;
        //初始化第0行,除了[0][0]全为false,毋庸置疑,因为空串p只能匹配空串,其他都无能匹配
        for (int i = 1; i <= m; i++)
            dp[i][0] = false;
        //初始化第0列,只有X*能匹配空串,如果有*,它的真值一定和p[0][j-2]的相同(略过它之前的符号)
        for (int j = 1; j <= n; j++)
            dp[0][j] = j > 1 && * == p[j - 1] && dp[0][j - 2];
?
        for (int i = 1; i <= m; i++){
            for (int j = 1; j <= n; j++){
                if (p[j - 1] == *){
                    //dp[i][j - 2] 是匹配0次的情况,例子:s=ab,p=aba*
                    //dp[i][j-1] 不匹配 例子:s=aba,p=aba*
                    //s[i - 1] == p[j - 2] && dp[i - 1][j]是匹配至少一次,例子:s=abb,p=ab*
                    //p[j - 2] == ‘.‘&& dp[i - 1][j]是匹配至少一次, 例子:s=abb,p=a.*     
                    dp[i][j] = dp[i][j - 2] || dp[i][j-1]||((s[i - 1] == p[j - 2] || p[j - 2] == .) && dp[i - 1][j]);
                }
                else{   
                    //p[j - 1] == ‘.‘ && dp[i - 1][j - 1] 例子:s=abb p=ab.
                    //s[i - 1] == p[j - 1] && dp[i - 1][j - 1]例子:s=abb p=abb
                    dp[i][j] = (p[j - 1] == . || s[i - 1] == p[j - 1]) && dp[i - 1][j - 1];
                }
            }
        }
        return dp[m][n];
    }
};

 

 

 

以上是关于10. Regular Expression Matching的主要内容,如果未能解决你的问题,请参考以下文章

10. Regular Expression Matching

10. Regular Expression Matching

10. Regular Expression Matching

10. Regular Expression Matching

10 Regular Expression Matching

10. Regular Expression Matching