@@ -3,43 +3,44 @@ class Solution:
33 def isMatch (self , s : str , p : str ) -> bool :
44 cache = [[False ] * (len (p ) + 1 ) for i in range (len (s ) + 1 )]
55 cache [len (s )][len (p )] = True
6-
6+
77 for i in range (len (s ), - 1 , - 1 ):
8- for j in range (len (p ) - 1 , - 1 , - 1 ):
8+ for j in range (len (p ) - 1 , - 1 , - 1 ):
99 match = i < len (s ) and (s [i ] == p [j ] or p [j ] == "." )
10-
10+
1111 if (j + 1 ) < len (p ) and p [j + 1 ] == "*" :
1212 cache [i ][j ] = cache [i ][j + 2 ]
1313 if match :
1414 cache [i ][j ] = cache [i + 1 ][j ] or cache [i ][j ]
1515 elif match :
16- cache [i ][j ] = cache [i + 1 ][j + 1 ]
17-
16+ cache [i ][j ] = cache [i + 1 ][j + 1 ]
17+
1818 return cache [0 ][0 ]
19-
20-
19+
20+
2121# TOP DOWN MEMOIZATION
2222class Solution :
2323 def isMatch (self , s : str , p : str ) -> bool :
2424 cache = {}
25-
25+
2626 def dfs (i , j ):
2727 if (i , j ) in cache :
2828 return cache [(i , j )]
2929 if i >= len (s ) and j >= len (p ):
3030 return True
3131 if j >= len (p ):
3232 return False
33-
33+
3434 match = i < len (s ) and (s [i ] == p [j ] or p [j ] == "." )
3535 if (j + 1 ) < len (p ) and p [j + 1 ] == "*" :
36- cache [(i , j )] = (dfs (i , j + 2 ) or # dont use *
37- (match and dfs (i + 1 , j ))) # use *
36+ cache [(i , j )] = dfs (i , j + 2 ) or ( # dont use *
37+ match and dfs (i + 1 , j )
38+ ) # use *
3839 return cache [(i , j )]
3940 if match :
40- cache [(i ,j )] = dfs (i + 1 , j + 1 )
41- return cache [(i ,j )]
42- cache [(i ,j )] = False
41+ cache [(i , j )] = dfs (i + 1 , j + 1 )
42+ return cache [(i , j )]
43+ cache [(i , j )] = False
4344 return False
44-
45+
4546 return dfs (0 , 0 )
0 commit comments