<filename>problems/zheng-ze-biao-da-shi-pi-pei-lcof/solution_2.py
class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        m, n = len(s), len(p)

        def matches(i: int, j: int) -> bool:
            if i == 0:
                return False
            if p[j - 1] == '.':
                return True
            return s[i-1] == p[j-1]

        f = [[False] * (n + 1) for _ in range(m + 1)]
        f[0][0] = True
        for i in range(m + 1):
            for j in range(1, n + 1):
                if p[j - 1] == '*':
                    f[i][j] |= f[i][j - 2]
                    if matches(i, j - 1):
                        f[i][j] |= f[i - 1][j]
                else:
                    if matches(i, j):
                        f[i][j] |= f[i - 1][j - 1]
        return f[m][n]


if __name__ == '__main__':
    test_cases = [["aa", "a"], ["aa", "a*"],
                  ["ab", ".*"], ["aab", "c*a*b"], ["mississippi", "mis*is*p*."]]
    for case in test_cases:
        ans = Solution().isMatch(*case)
        print(ans)

<filename>problems/zheng-ze-biao-da-shi-pi-pei-lcof/solution_2.py
class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        m, n = len(s), len(p)

        def matches(i: int, j: int) -> bool:
            if i == 0:
                return False
            if p[j - 1] == '.':
                return True
            return s[i-1] == p[j-1]

        f = [[False] * (n + 1) for _ in range(m + 1)]
        f[0][0] = True
        for i in range(m + 1):
            for j in range(1, n + 1):
                if p[j - 1] == '*':
                    f[i][j] |= f[i][j - 2]
                    if matches(i, j - 1):
                        f[i][j] |= f[i - 1][j]
                else:
                    if matches(i, j):
                        f[i][j] |= f[i - 1][j - 1]
        return f[m][n]


if __name__ == '__main__':
    test_cases = [["aa", "a"], ["aa", "a*"],
                  ["ab", ".*"], ["aab", "c*a*b"], ["mississippi", "mis*is*p*."]]
    for case in test_cases:
        ans = Solution().isMatch(*case)
        print(ans)
