Skip to content

Commit d402619

Browse files
(LEETCODE)Regular _Expression_ Matching.py
1 parent 4061208 commit d402619

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Solution to https://leetcode.com/problems/regular-expression-matching
2+
3+
class Solution:
4+
def isMatch(self, s: str, p: str) -> bool:
5+
cache={}
6+
def dfs(i,j):
7+
if (i,j) in cache:
8+
return cache[(i,j)]
9+
if i>=len(s) and j>=len(p):
10+
return True
11+
if j>=len(p):
12+
return False
13+
match=i<len(s) and(s[i]==p[j] or p[j]==".")
14+
if (j+1)<len(p) and p[j+1]=="*":
15+
cache[(i,j)]=(dfs(i,j+2) or (match and dfs(i+1,j)))
16+
return cache[(i,j)]
17+
if match:
18+
cache[(i,j)]=dfs(i+1,j+1)
19+
return cache[(i,j)]
20+
cache[(i,j)]=False
21+
return False
22+
return dfs(0,0)

0 commit comments

Comments
 (0)