python
class Solution:
def isMatch(self, s: str, p: str):
m, n = len(s), len(p)
map = dict()
def dp(s, p, i, j):
if j == n:
return i == m
if i == m:
if (n-j) % 2 == 1:
return False
else:
for k in range(j, n, 2):
if p[k+1] != '*':
return False
return True
key = (i, j)
if key in map.keys():
return map[key]
res = False
if s[i] == p[j] or p[j] == '.':
if j < n-1 and p[j+1] == '*':
res = dp(s, p, i, j+2) or dp(s, p, i+1, j)
else:
res = dp(s, p, i+1, j+1)
else:
if j < n-1 and p[j+1] == '*':
res = dp(s, p, i, j+2)
else:
res = False
map[key] = res
return res
return dp(s, p, 0, 0)