【leetcode】10.regular-expression-matching(正则表达式匹配)

【leetcode】10.regular-expression-matching(正则表达式匹配)

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

10. 正则表达式匹配

给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配。

'.' 匹配任意单个字符
'*' 匹配零个或多个前面的那一个元素
所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。

说明:

s 可能为空,且只包含从 a-z 的小写字母。
p 可能为空,且只包含从 a-z 的小写字母,以及字符 . 和 *。
示例 1:

输入:
s = "aa"
p = "a"
输出: false
解释: "a" 无法匹配 "aa" 整个字符串。
示例 2:

输入:
s = "aa"
p = "a*"
输出: true
解释: 因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。
示例 3:

输入:
s = "ab"
p = ".*"
输出: true
解释: ".*" 表示可匹配零个或多个('*')任意字符('.')。
示例 4:

输入:
s = "aab"
p = "c*a*b"
输出: true
解释: 因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。
示例 5:

输入:
s = "mississippi"
p = "mis*is*p*."
输出: false

思路:

题意分析:

  1. dp[i][j] 表示 p[0...i-1]s[0...j-1] 能否匹配上。
  2. 如果 dp[i][j] 能匹配,那么当 p[i]==s[j]p[i]=='.'dp[i+1][j+1] 能匹配。
  3. 由此,可知 dp[0][0] = 1p 空串s 空串 能匹配。
  4. 如果没有 '*' ,那么其它字符匹配方式就是:

    dp[i+1][j+1] = dp[i][j] && (p[i] == s[j] || p[i] == '.')
  5. '*'的特殊处理:
  • I. 取 0 时,相当于 p[i] 和 p[i-1] 被抹除了,dp[i+1][…] 状态等价于 dp[i-1][…]

    dp[i+1][…] = dp[i-1][…]
  • II. 取 1 时,相当于 p[i] 被抹除了,dp[i+1][…] 状态等价于 dp[i][…]

    dp[i+1][...] = dp[i][...]
  • III. 取 infinite 时,相当于 infinitep[i-1],相当于 (4) 的方式 + 如果前一个 dp[i+1][j] 能匹配,那么可以继续匹配下去

    dp[i+1][j+1] = (dp[i][j] || dp[i+1][j]) && (p[i] == s[j] || p[i] == '.')
  • '*' 的状态取上面 3 钟之和。
  1. 由上述可知,最多需要用到前面 2 组状态,所以申请内存时只需要申请 3 * (slen + 1) 即可。轮流切换状态复用即可。

例子

  s = "abcdede";
  p = "ab.*ee*de";

           _ a b c d e d e
[ -1 ] _ : 1 0 0 0 0 0 0 0
[  0 ] a : 0 1 0 0 0 0 0 0
[  1 ] b : 0 0 1 0 0 0 0 0
[  2 ] . : 0 0 0 1 0 0 0 0
[  3 ] * : 0 0 1 1 1 1 1 1
[  4 ] e : 0 0 0 0 0 1 0 1
[  5 ] e : 0 0 0 0 0 0 0 0
[  6 ] * : 0 0 0 0 0 1 0 1
[  7 ] d : 0 0 0 0 0 0 1 0
[  8 ] e : 0 0 0 0 0 0 0 1

CODE:

class Solution {
public:
    bool isMatch(string s, string p) {
        const int slen = s.length();
        const int plen = p.length();

        vector<vector<int>> dp(3, vector<int>(slen + 1));
        int pre2_idx = 0, pre_idx = 1, cur_idx = 2;
        dp[pre_idx][0] = 1;
        int pre_cnt = 1, cur_cnt = 0;
        for (int i = 0; i < plen; ++i) {
            for (int j=0; j<=slen; ++j) {
                dp[cur_idx][j] = 0;
            }
            char ch = p[i];
            cur_cnt = 0;
            bool bIsStar = false;
            if (ch == '*') {
                bIsStar = true;
                ch = p[i-1];
                for (int j=0; j<=slen; ++j) {
                    dp[cur_idx][j] = (dp[pre_idx][j] || dp[pre2_idx][j]) ? 1 : 0;
                }
                cur_cnt = 1;
            }
            for (int j=0; j<slen; ++j) {
                if ((dp[pre_idx][j] || (bIsStar && dp[cur_idx][j]))
                     && (ch == '.' || ch == s[j])) {
                    dp[cur_idx][j+1] = 1;
                    ++cur_cnt;
                }
            }
            if (!cur_cnt && !pre_cnt) {
                return false;
            }
            pre_cnt = cur_cnt;
            pre2_idx = pre_idx;
            pre_idx = cur_idx;
            cur_idx = (cur_idx+1) % 3;
        }

        return dp[pre_idx][slen];
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值