Skip to content

Commit 18353d3

Browse files
committed
added leetcode Wildcard matching python code
1 parent 55ea0cc commit 18353d3

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

wildcardmatching.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def isMatch(self, s: str, p: str) -> bool:
3+
m, n = len(s), len(p)
4+
5+
# dp[i][j] = does s[:i] match p[:j]
6+
dp = [[False] * (n + 1) for _ in range(m + 1)]
7+
dp[0][0] = True
8+
9+
# Handle patterns like *, **, ***, they match empty string
10+
for j in range(1, n + 1):
11+
if p[j - 1] == '*':
12+
dp[0][j] = dp[0][j - 1]
13+
14+
for i in range(1, m + 1):
15+
for j in range(1, n + 1):
16+
if p[j - 1] == s[i - 1] or p[j - 1] == '?':
17+
dp[i][j] = dp[i - 1][j - 1]
18+
elif p[j - 1] == '*':
19+
dp[i][j] = dp[i][j - 1] or dp[i - 1][j]
20+
21+
return dp[m][n]

0 commit comments

Comments
 (0)