Compare commits

..

No commits in common. "475b5b5184bbf5ec91bdcd5a4afe937223ec5536" and "b87b9a819fb88f499ecd0df2f441bcb9e8c359a7" have entirely different histories.

View File

@ -30,11 +30,17 @@ def recursive_match(text: str, pattern: str) -> bool:
>>> recursive_match('aa', '.*')
True
"""
if not pattern:
return not text
if not text and not pattern:
return True
if not text:
return pattern[-1] == "*" and recursive_match(text, pattern[:-2])
if text and not pattern:
return False
if not text and pattern and pattern[-1] != "*":
return False
if not text and pattern and pattern[-1] == "*":
return recursive_match(text, pattern[:-2])
if text[-1] == pattern[-1] or pattern[-1] == ".":
return recursive_match(text[:-1], pattern[:-1])
@ -74,16 +80,19 @@ def dp_match(text: str, pattern: str) -> bool:
dp = [[False for _ in range(n + 1)] for _ in range(m + 1)]
dp[0][0] = True
for i in range(1, m + 1):
dp[i][0] = False
for j in range(1, n + 1):
dp[0][j] = pattern[j - 1] == "*" and dp[0][j - 2]
for i in range(1, m + 1):
for j in range(1, n + 1):
if pattern[j - 1] in {".", text[i - 1]}:
if pattern[j - 1] == "." or pattern[j - 1] == text[i - 1]:
dp[i][j] = dp[i - 1][j - 1]
elif pattern[j - 1] == "*":
dp[i][j] = dp[i][j - 2]
if pattern[j - 2] in {".", text[i - 1]}:
if pattern[j - 2] == "." or pattern[j - 2] == text[i - 1]:
dp[i][j] |= dp[i - 1][j]
else:
dp[i][j] = False