正則表達式匹配-力扣

在這裏插入圖片描述

考察知識點

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章