forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10-Regular-Expression-Matching.py
45 lines (37 loc) · 1.55 KB
/
10-Regular-Expression-Matching.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# BOTTOM-UP Dynamic Programming
class Solution:
def isMatch(self, s: str, p: str) -> bool:
cache = [[False] * (len(p) + 1) for i in range(len(s) + 1)]
cache[len(s)][len(p)] = True
for i in range(len(s), -1, -1):
for j in range(len(p) - 1, -1 ,-1):
match = i < len(s) and (s[i] == p[j] or p[j] == ".")
if (j + 1) < len(p) and p[j + 1] == "*":
cache[i][j] = cache[i][j + 2]
if match:
cache[i][j] = cache[i + 1][j] or cache[i][j]
elif match:
cache[i][j] = cache[i+1][j+1]
return cache[0][0]
# TOP DOWN MEMOIZATION
class Solution:
def isMatch(self, s: str, p: str) -> bool:
cache = {}
def dfs(i, j):
if (i, j) in cache:
return cache[(i, j)]
if i >= len(s) and j >= len(p):
return True
if j >= len(p):
return False
match = i < len(s) and (s[i] == p[j] or p[j] == ".")
if (j + 1) < len(p) and p[j + 1] == "*":
cache[(i, j)] = (dfs(i, j + 2) or # dont use *
(match and dfs(i + 1, j))) # use *
return cache[(i, j)]
if match:
cache[(i,j)] = dfs(i + 1, j + 1)
return cache[(i,j)]
cache[(i,j)] = False
return False
return dfs(0, 0)