Leetcode 10 正则表达式匹配
给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 ‘.’ 和 ‘*’ 的正则表达式匹配。
def isMatch(self, s: str, p: str) -> bool:
#考虑边界条件
if not p: return not s
#考虑第一个匹配点
first_match = (len(s) > 0) and p[0] in {s[0], '.'}
# 先处理 `*`
if len(p) >=2 and p[1] == '*':
# 匹配0个 或 多个
return self.isMatch(s, p[2:]) or (first_match and self.isMatch(s[1:], p))
# 处理 `.`
return first_match and self.isMatch(s[1:], p[1:])