【leetcode】正则表达式匹配【python】【递归,后序遍历】

题目地址
在这里插入图片描述

class Solution(object):
    def myMatch(self, s, p, s_index, p_index):
        print(s_index, p_index)
        if p == '' and s != '':
            #print(1)
            return False
        if s == '':
            if p == '' or (len(p) >=2 and p[1] == '*'):
                return True
            else:
                print(2)
                return False
        if p_index == -1:
            if s_index == -1:
                return True
            else:
                print(3)
                return False
        if s_index == -1:
            if p_index == 0:
                if s[0] != p[0]:
                    print(4)
                    return False
                # elif s[0] == p[0]:
                #     return True
            else:
                return self.myMatch(s, p, s_index, p_index - 2)
        if p[p_index] == '*':
            if s_index > -1 and (p[p_index - 1] == '.' or (s[s_index] == p[p_index - 1] and s[s_index - 1] != p[p_index - 2] and p[p_index - 2] != '*')):
                return self.myMatch(s, p, s_index - 1, p_index)
            else:
                return self.myMatch(s, p, s_index, p_index - 2)
        else:
            if p[p_index] == '.' or p[p_index] == s[s_index]:
                return self.myMatch(s, p, s_index - 1, p_index - 1)
            else:
                print(5)
                return False


    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        return self.myMatch(s, p, len(s) - 1, len(p) - 1)

sol = Solution()
print(sol.isMatch('aaa', 'ab*a*c*a'))

这样无法识别’aaa’ 与 ‘abaca’这种,因为无法识别出,到底遇到*的时候,移动几个字母
改进版

class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        # 判断匹配规则是否为空
        if p == "":
            # p为空的时候,判断s是否为空,则知道返回True 或 False
            return s == ""
        # 判断匹配规则是否只有一个
        if len(p) == 1:
            # 判断匹配字符串长度是否为1,和两者的第一个元素是否相同,或匹配规则使用.
            return len(s) == 1 and (s[0] == p[0] or p[0] == '.')
        # 匹配规则的第二个字符串不为*,当匹配字符串不为空的时候
        # 返回 两者的第一个元素是否相同,或匹配规则使用. and 递归新的字符串(去掉第一个字符的匹配字符串 和 去掉第一个字符的匹配规则)
        if p[1] != "*":
            if s == "":
                return False
            return (s[0] == p[0] or p[0] == '.') and self.isMatch(s[1:], p[1:])
        # 当匹配字符串不为空 and (两者的第一个元素是否相同 or 匹配规则使用.)
        while s and (s[0] == p[0] or p[0] == '.'):
            # 到了while循环,说明p[1]为*,所以递归调用匹配s和p[2:](*号之后的匹配规则)
            # 用于跳出函数,当s循环到和*不匹配的时候,则开始去匹配p[2:]之后的规则
            if self.isMatch(s, p[2:]):
                return True
            # 当匹配字符串和匹配规则*都能匹配的时候,去掉第一个字符成为新的匹配字符串,循环
            s = s[1:]
        # 假如第一个字符和匹配规则不匹配,则去判断之后的是否匹配
        return self.isMatch(s, p[2:])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值