Участник:Danillich/RegularExpressionMatching

Материал из DISCOPAL
Перейти к: навигация, поиск
class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        s, p = ' ' + s, ' ' + p
        s_l, p_l = len(s), len(p)
        dp = [[False]*(p_l) for _ in range(s_l)]
        dp[0][0] = True
 
        for j in range(1, p_l):
            if p[j] == '*':
                dp[0][j] = dp[0][j-2]
 
        for i in range(1, s_l):
            for j in range(1, p_l):
                if p[j] in {s[i], '.'}:
                    dp[i][j] = dp[i-1][j-1]
                elif p[j] == "*":
                    dp[i][j] = dp[i][j-2] or (dp[i-1][j] and p[j-1] in {s[i], '.'})
 
        return dp[-1][-1]