Skip to content

Commit 019b886

Browse files
committed
Solved leetcode 10
1 parent da60996 commit 019b886

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

10/10.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def isMatch(self, s: str, p: str) -> bool:
3+
4+
cache = dict()
5+
6+
def helper(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+
elif(j >= len(p)):
12+
return False
13+
14+
charMatch = i < len(s) and (s[i] == p[j] or p[j] == '.')
15+
if j + 1 < len(p) and p[j + 1] == '*':
16+
cache[(i, j)] = helper(i, j + 2) or (charMatch and helper(i + 1, j))
17+
return cache[(i, j)]
18+
19+
if charMatch:
20+
cache[(i, j)] = helper(i + 1, j + 1)
21+
return cache[(i, j)]
22+
23+
cache[(i, j)] = False
24+
return False
25+
26+
return helper(0,0)
27+

0 commit comments

Comments
 (0)