Участник:Alexander Denisenko/Wildcard matching

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