Skip to content

Commit 7e3961b

Browse files
committed
feat(leetcode): add No.10
1 parent 5cc6faa commit 7e3961b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

10.Regular-Expression-Matching.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# https://leetcode.com/problems/regular-expression-matching/
2+
#
3+
# algorithms
4+
# Hard (25.4%)
5+
# Total Accepted: 315,936
6+
# Total Submissions: 1,243,801
7+
8+
9+
class Solution(object):
10+
def isMatch(self, s, p):
11+
"""
12+
:type s: str
13+
:type p: str
14+
:rtype: bool
15+
"""
16+
s_len, p_len = len(s), len(p)
17+
dp = [[False] * (p_len + 1) for _ in xrange(s_len + 1)]
18+
19+
dp[0][0] = True
20+
for i in xrange(2, p_len + 1):
21+
if p[i - 1] == '*' and dp[0][i - 2]:
22+
dp[0][i] = True
23+
24+
for i in xrange(s_len):
25+
for j in xrange(p_len):
26+
if s[i] == p[j] or p[j] == '.':
27+
dp[i + 1][j + 1] = dp[i][j]
28+
29+
elif p[j] == '*':
30+
if p[j - 1] == s[i] or p[j - 1] == '.':
31+
dp[i + 1][j + 1] = dp[i + 1][j -
32+
1] or dp[i + 1][j] or dp[i][j + 1]
33+
else:
34+
dp[i + 1][j + 1] = dp[i + 1][j - 1]
35+
36+
return dp[s_len][p_len]

0 commit comments

Comments
 (0)