26. Wildcard Pattern Matching
The problem can be found at the following link: Question Link
Note: Sorry for uploading late, my exam is going on.
Problem Description
Given two strings, pattern
and str
, which may be of different sizes, return 1
if the wildcard pattern matches str
, else return 0
. The wildcard pattern can include the characters ?
and *
.
?
matches any single character.*
matches any sequence of characters (including the empty sequence).
Note: The matching should cover the entire string str
(not partial matches).
Example:
Input:
pattern = "ba*a?"
str = "baaabab"
Output:
1
Explanation: Replace *
with "aab"
and ?
with "b"
.
My Approach
Dynamic Programming (DP) Setup:
Create a 2D DP array where
dp[i][j]
will be1
if the firsti
characters inpattern
match the firstj
characters instr
, else0
.
Initialization:
dp[0][0] = 1
because an empty pattern matches an empty string.If
pattern[j-1]
is*
, thendp[j]
can inherit the value fromdp[j-1]
since*
can represent an empty sequence.
DP Array Population:
For each character in
str
andpattern
, update the DP array:If
pattern[j-1]
is?
orpattern[j-1]
matchesstr[i-1]
, then setnewDp[j] = dp[j-1]
.If
pattern[j-1]
is*
, setnewDp[j] = dp[j] || newDp[j-1]
, allowing*
to match multiple characters.
Final Answer:
The value of
dp[n]
will determine if the entirestr
matches thepattern
.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(n*m), where
n
is the length ofstr
andm
is the length ofpattern
, as we iterate over all possible pairs of characters.Expected Auxiliary Space Complexity: O(n*m), due to the DP array that stores matching results for all substrings.
Code (C++)
class Solution {
public:
int wildCard(string pattern, string str) {
int m = str.length();
int n = pattern.length();
vector<int> dp(n + 1, 0);
dp[0] = 1;
for (int j = 1; j <= n; j++) {
if (pattern[j - 1] == '*') {
dp[j] = dp[j - 1];
}
}
for (int i = 1; i <= m; i++) {
vector<int> newDp(n + 1, 0);
for (int j = 1; j <= n; j++) {
if (pattern[j - 1] == '?' || pattern[j - 1] == str[i - 1]) {
newDp[j] = dp[j - 1];
} else if (pattern[j - 1] == '*') {
newDp[j] = dp[j] || newDp[j - 1];
}
}
dp.swap(newDp);
}
return dp[n];
}
};
Code (Java)
class Solution {
public int wildCard(String pattern, String str) {
int m = str.length();
int n = pattern.length();
int[] dp = new int[n + 1];
dp[0] = 1;
for (int j = 1; j <= n; j++) {
if (pattern.charAt(j - 1) == '*') {
dp[j] = dp[j - 1];
}
}
for (int i = 1; i <= m; i++) {
int[] newDp = new int[n + 1];
for (int j = 1; j <= n; j++) {
if (pattern.charAt(j - 1) == '?' || pattern.charAt(j - 1) == str.charAt(i - 1)) {
newDp[j] = dp[j - 1];
} else if (pattern.charAt(j - 1) == '*') {
newDp[j] = dp[j] | newDp[j - 1];
}
}
dp = newDp;
}
return dp[n];
}
}
Code (Python)
class Solution:
def wildCard(self, pattern, string):
m = len(string)
n = len(pattern)
dp = [0] * (n + 1)
dp[0] = 1
for j in range(1, n + 1):
if pattern[j - 1] == '*':
dp[j] = dp[j - 1]
for i in range(1, m + 1):
new_dp = [0] * (n + 1)
for j in range(1, n + 1):
if pattern[j - 1] == '?' or pattern[j - 1] == string[i - 1]:
new_dp[j] = dp[j - 1]
elif pattern[j - 1] == '*':
new_dp[j] = dp[j] or new_dp[j - 1]
dp = new_dp
return dp[n]
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit my LinkedIn:- Any Questions. Thank you for your input; together, we strive to create a space where learning is a collaborative endeavor.
β Star this repository if you find it helpful or intriguing! β
πVisitor Count
Last updated