Files
exams/algorithms/labs/lab6/leetcode/1/solution.py
2025-10-01 22:55:09 +03:00

15 lines
483 B
Python

class Solution:
def isMatch(self, s: str, p: str) -> bool:
from functools import lru_cache
@lru_cache(None)
def dp(i: int, j: int) -> bool:
if j == len(p):
return i == len(s)
first = i < len(s) and (p[j] == s[i] or p[j] == ".")
if j + 1 < len(p) and p[j + 1] == "*":
return dp(i, j + 2) or (first and dp(i + 1, j))
return first and dp(i + 1, j + 1)
return dp(0, 0)