forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0044.py
More file actions
24 lines (21 loc) · 882 Bytes
/
0044.py
File metadata and controls
24 lines (21 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
def isMatch(self, s: str, p: str) -> bool:
m = len(s)
n = len(p)
dp = [[False for j in range(n + 1)] for i in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0 and j == 0:
dp[i][j] = True
elif i == 0:
dp[i][j] = dp[i][j - 1] and p[j - 1] == '*'
elif j == 0:
dp[i][j] = dp[i - 1][j] and s[i - 1] == '*'
else:
dp[i][j] = \
(dp[i - 1][j] or dp[i][j - 1] or dp[i - 1][j - 1]) and \
(s[i - 1] == '*' or p[j - 1] == '*') or \
(dp[i - 1][j - 1]) and \
(s[i - 1] == '?' or p[j - 1] == '?' or
s[i - 1] == p[j - 1])
return dp[m][n]