刷题--正则表达式匹配

请实现一个函数用来匹配包括’.’和’*’的正则表达式。模式中的字符’.’表示任意一个字符,而’*’表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串”aaa”与模式”a.a”和”ab*ac*a”匹配,但是与”aa.a”和”ab*a”均不匹配

基本思路:递归,根据模式中第二个字符是不是”*”分两种情况讨论。
如果模式中第二个字符不是”*”:
1.如果字符串中第一个字符和模式中第一个字符相匹配,那么字符串和模式都后移一位。
2.如果字符串中第一个字符和模式中第一个字符不匹配,那么返回False。
如果模式中第二个字符是”*”:
1.如果字符串中第一个字符和模式中第一个字符相匹配:
(1)匹配一次,字符串后移一位,模式后移两位。
(2)匹配多次,字符串后移一位,模式不变。
(3)忽略,字符串不变,模式后移两位。
2.如果字符串中第一个字符和模式中第一个字符不匹配,那么模式后移两位。

# -*- coding:utf-8 -*-
class Solution:
    # s, pattern都是字符串
    def matchCore(self, s, sIndex, pattern, pIndex):
        if sIndex == len(s) and pIndex == len(pattern):
            return True

        if sIndex != len(s) and pIndex == len(pattern):
            return False

        if pIndex + 1 < len(pattern) and pattern[pIndex + 1] == '*':
            if sIndex < len(s) and (s[sIndex] == pattern[pIndex] or pattern[pIndex] == '.'):
                return self.matchCore(s, sIndex + 1, pattern, pIndex + 2) or self.matchCore(s, sIndex + 1, pattern, pIndex) or self.matchCore(s, sIndex, pattern, pIndex + 2)
            else:
                return self.matchCore(s, sIndex, pattern, pIndex + 2)

        if sIndex < len(s) and (s[sIndex] == pattern[pIndex] or pattern[pIndex] == '.'):
            return self.matchCore(s, sIndex + 1, pattern, pIndex + 1)

        return False


    def match(self, s, pattern):
        # write code here
        sIndex, pIndex = 0, 0
        return self.matchCore(s, sIndex, pattern, pIndex)

注意要时刻考虑数组是否越界。

升级版:动态规划。

# -*- coding:utf-8 -*-
class Solution:
    # s, pattern都是字符串
    def match(self, s, pattern):
        # write code here
        dp = [[False] * (len(pattern) + 1) for _ in range(len(s) + 1)]

        dp[-1][-1] = True
        for i in range(len(s), -1, -1):
            for j in range(len(pattern) - 1, -1, -1):
                first_match = i < len(s) and pattern[j] in {s[i], '.'}

                if j + 1 < len(pattern) and pattern[j + 1] == '*':
                    dp[i][j] = dp[i][j + 2] or first_match and dp[i + 1][j]# or first_match and dp[i + 1][j + 2]
                else:
                    dp[i][j] = first_match and dp[i + 1][j + 1]

        return dp[0][0]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值