今天的题目是10. 正则表达式匹配
class Solution(object):
def isMatch(self, text, pattern):
memo = {}
def dp(i, j):
if (i, j) not in memo:
if j == len(pattern):
ans = i == len(text)
else:
first_match = i < len(text) and pattern[j] in {text[i], '.'}
if j+1 < len(pattern) and pattern[j+1] == '*':
ans = dp(i, j+2) or first_match and dp(i+1, j)
else:
ans = first_match and dp(i+1, j+1)
memo[i, j] = ans
return memo[i, j]
return dp(0, 0)
执行用时 :80 ms, 在所有 Python3 提交中击败了52.64%的用户
内存消耗 :13.6 MB, 在所有 Python3 提交中击败了5.13%的用户
动态规划 啊啊啊