forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0010.py
23 lines (20 loc) · 749 Bytes
/
0010.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
def isMatch(self, s, p):
mem = {}
def _isMatch(i, j):
if (i, j) not in mem:
if j == len(p):
result = i == len(s)
else:
first_match = i < len(s) and p[j] in (s[i], '.')
if j + 1 < len(p) and p[j+1] == '*':
result = _isMatch(i, j+2) or (first_match and _isMatch(i+1, j))
else:
result = first_match and _isMatch(i+1, j+1)
mem[i, j] = result
return mem[i, j]
return _isMatch(0,0)
if __name__ == "__main__":
s = "baccbbcbcacacbbc"
p = "c*.*b*c*ba*b*b*.a*"
print(Solution().isMatch(s, p))