From 18353d3b91459ef69faba46dadcb20cce9f1f483 Mon Sep 17 00:00:00 2001 From: manogna7s Date: Sat, 25 Oct 2025 20:42:16 +0530 Subject: [PATCH] added leetcode Wildcard matching python code --- wildcardmatching.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 wildcardmatching.py diff --git a/wildcardmatching.py b/wildcardmatching.py new file mode 100644 index 00000000..b9ad70ed --- /dev/null +++ b/wildcardmatching.py @@ -0,0 +1,21 @@ +class Solution: + def isMatch(self, s: str, p: str) -> bool: + m, n = len(s), len(p) + + # dp[i][j] = does s[:i] match p[:j] + dp = [[False] * (n + 1) for _ in range(m + 1)] + dp[0][0] = True + + # Handle patterns like *, **, ***, they match empty string + for j in range(1, n + 1): + if p[j - 1] == '*': + dp[0][j] = dp[0][j - 1] + + for i in range(1, m + 1): + for j in range(1, n + 1): + if p[j - 1] == s[i - 1] or p[j - 1] == '?': + dp[i][j] = dp[i - 1][j - 1] + elif p[j - 1] == '*': + dp[i][j] = dp[i][j - 1] or dp[i - 1][j] + + return dp[m][n]