-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10. Regular Expression Matching.py
74 lines (69 loc) · 2.36 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Solution:
def isMatch(self, s: str, p: str) -> bool:
return self.matchFunc(s, p, "")
def matchFunc(self, s: str, p: str, prev: str) -> bool:
print("s:{}, p:{}".format(s, p))
if s == "":
if p == "":
return True
else:
while len(p) >= 2:
p1 = p[0]
p2 = p[1]
if ("z" >= p1 >= "a" or p1 == ".") and p2 == "*":
p = p[2:]
else:
return False
return True if len(p) == 0 else False
elif p == "":
return False
pattern = p[0]
if len(p) >= 2 and p[1] == "*":
prev = p[0]
compare = prev
if prev == ".":
compare = s[0]
if s[0] != compare:
return self.matchFunc(s, p[2:], prev)
else:
if not self.matchFunc(s[1:], p, prev):
return self.matchFunc(s, p[2:], prev)
else:
return True
else:
if "z" >= pattern >= "a":
if s[0] == pattern:
return self.matchFunc(s[1:], p[1:], s[0])
else:
return False
if pattern == ".":
return self.matchFunc(s[1:], p[1:], s[0])
else:
return False
def isMatchFaster(self, s: str, p: str) -> bool: # turned the backtrack recursion to iteration
def match(i: int, j: int) -> bool:
if i == 0:
return False
if p[j - 1] == ".":
return True
else:
return s[i - 1] == p[j - 1]
m, n = len(s), len(p)
dp = [[False] * (n + 1) for _ in range(m + 1)]
dp[0][0] = True
for i in range(m + 1):
for j in range(1, n + 1):
if p[j - 1] == "*":
dp[i][j] = dp[i][j - 2]
dp[i][j] |= dp[i - 1][j] if match(i, j - 1) else dp[i][j]
else:
dp[i][j] = dp[i - 1][j - 1] if match(i, j) else dp[i][j]
return dp[m][n]
testS3 = "aabcbcbcaccbcaabc"
testP3 = ".*a*aa*.*b*.c*.*a*"
testS = "mississippi"
testP = "mis*is*p*."
testS2 = "aaa"
testP2 = "a*a"
sol = Solution()
print(sol.isMatchFaster(testS3, testP3))