Login dark

https://leetcode-cn.com/problems/regular-expression-matching/submissions/

参考:由浅入深,详细讲解

class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        if p == '':
            return s == ''
        
        #第一个字符是否匹配
        match_first = s != '' and p[0] in ['.',(s[0] if len(s) > 0 else '')]
        if len(p) > 1 and p[1] == '*':
            #*情况 分别判断:1.匹配当前字符,模式原样传递 2.不匹配当前字符,模式删除
            return (match_first and self.isMatch(s[1:],p)) or self.isMatch(s,p[2:])
        else:
            #其他情况
            return match_first and self.isMatch(s[1:],p[1:])