File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
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 ]
You can’t perform that action at this time.
0 commit comments