考察知識點
python 動態規劃 回溯算法
代碼
class Solution(object):
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
memo={}
def dp(i,j):
if (i,j) in memo:return memo[(i,j)]
if j==len(p):return i==len(s)
if i>len(s):return False
match=i<len(s) and p[j] in (s[i],".")
if j <=len(p)-2 and p[j+1]=="*":
res=dp(i,j+2) or (match and dp(i+1,j))
else:
res=match and dp(i+1,j+1)
memo[(i,j)]=res
return res
return dp(0,0)
參考https://leetcode-cn.com/problems/regular-expression-matching/solution/ji-yu-guan-fang-ti-jie-gen-xiang-xi-de-jiang-jie-b/#comment