-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.4.py
More file actions
27 lines (26 loc) · 932 Bytes
/
2.4.py
File metadata and controls
27 lines (26 loc) · 932 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
25
26
27
def isMatch(s: str, p: str) -> bool:
rows, columns = (len(s), len(p))
# Base conditions
if rows == 0 and columns == 0:
return True
if columns == 0:
return False
# DP array
dp = [[False for j in range(columns + 1)] for i in range(rows + 1)]
# Since empty string and empty pattern are a match
dp[0][0] = True
# Deals with patterns containing *
for i in range(2, columns + 1):
if p[i - 1] == '*':
dp[0][i] = dp[0][i - 2]
# For remaining characters
for i in range(1, rows + 1):
for j in range(1, columns + 1):
if s[i - 1] == p[j - 1] or p[j - 1] == '.':
dp[i][j] = dp[i - 1][j - 1]
elif j > 1 and p[j - 1] == '*':
dp[i][j] = dp[i][j - 2]
if p[j - 2] == '.' or p[j - 2] == s[i - 1]:
dp[i][j] = dp[i][j] or dp[i - 1][j]
return dp[rows][columns]
print(isMatch("a", "aa"))