leetcode 10 Regular Expression Matching解读

Given an input string (s) and a pattern §, 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 2:

Input:
s = “aa”
p = “a*”
Output: true
Explanation: ‘*’ means zero or more of the precedeng 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 (.)”.

正则匹配问题

出错点:
string的长度是length(), 数组的长度是length
charAt() 写成了 charAt[]

思路:

dynamic programming, 定义一个dp矩阵result, boolean type
i代表s的index,j代表p的index
矩阵的行对应s的index i,列对应p的index j

首先result[0][0] = true;
因为0代表字符串长度为0,也就是s和p都是空字符串是应该是匹配的。

第0列代表p是空字符串,这时候从第一行开始,和s的每一个都不匹配,所以
i from 1 to s.length
result[i][0] = false;

第0行代表s是空字符串,p中不是“*”时是false,是“ * “时看它前面一个不出现时的匹配
result[0][j] = result[0][j-2]

然后从第1行第1列起到最后
情形1:
s[i] == p[j] 或者p[j] == ‘.’
这个时候s的当前字符和p的当前字符一样,所以只要之前的也匹配,到i和j也就匹配
所以result[i][j] = result[i-1][j-1]

情形2:
情形1不满足,但是p[j] == ‘*’
情形2-1:
s=“a“, p=”ab * ”
因为 *代表0个或多个它前面的字符,所以这时候 *只要代表0个b就匹配
这种情况下
result[i][j] = result[i][j-2]
情形2-2:
s = “aa”, p= “a *”
这个时候把s中第二个a看作是p中"a *“的一部分,也就是说s中第二个a不出现也不影响结果,所以只需要考虑"a *“和第二个a的前一个是否匹配。
result[i][j] = result[i-1][j]
“a *”和前一个匹配的条件就是要么 *的前一个是”.”, 要么就是 *的前一个a和s中第一个也匹配
p[j-1] == ‘.’ or s[i-1] == p[j-1]

因为情形2中2-1和2-2只要有一个匹配,整体就匹配
所以情形2-1和2-2应该是或的关系

代码如下:

    public boolean isMatch(String s, String p) {
        if (s == null || p == null) {
            return false;
        }
        
        boolean[][] result = new boolean[s.length() + 1][p.length() + 1];
        
        result[0][0] = true;
        
        for(int i = 1; i < p.length() + 1; i++) {
            if (p.charAt(i-1) == '*') {
                result[0][i] = result[0][i-2];
            }
        }
        
        for(int i = 1; i < s.length() + 1; i++) {
            for(int j = 1; j < p.length() + 1; j++) {
                if (s.charAt(i-1) == p.charAt(j-1) || p.charAt(j-1) == '.') {
                    result[i][j] = result[i-1][j-1];
                } else if (p.charAt(j-1) == '*') {
                    result[i][j] = result[i][j-2];
                    if (s.charAt(i-1) == p.charAt(j-2) || p.charAt(j-2) == '.') {
                        result[i][j] |= result[i-1][j];
                    }
                }
            }
        }
        
        return result[s.length()][p.length()];
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值