12. Wildcard Pattern Matching
β GFG solution to the Wildcard Pattern Matching problem: determine if a pattern with wildcards ('?' and '*') matches a given text using dynamic programming. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given two strings txt and pat which may be of different sizes, you have to return true if the wildcard pattern pat matches with txt, else return false.
The wildcard pattern pat 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 txt (not partial txt).
π Examples
Example 1
Input: txt = "abcde", pat = "a?c*"
Output: true
Explanation: '?' matches with 'b' and '*' matches with "de".Example 2
Input: txt = "baaabab", pat = "a*ab"
Output: false
Explanation: The pattern starts with 'a', but the text starts with 'b',
so the pattern does not match the text.Example 3
Input: txt = "abc", pat = "*"
Output: true
Explanation: '*' matches with whole text "abc".π Constraints
$1 \le \text{txt.size()}, \text{pat.size()} \le 100$
β
My Approach
The optimal approach uses Dynamic Programming with a 2D table to efficiently determine if the pattern matches the text:
Bottom-Up Dynamic Programming
Initialize DP Table:
Create a 2D boolean array
dp[n+1][m+1]wherenis the length of text andmis the length of pattern.dp[i][j]represents whether firsticharacters of text match firstjcharacters of pattern.Set
dp[0][0] = true(empty pattern matches empty text).
Handle Leading Stars:
For pattern positions with
'*', if previous position matched empty text, current position also matches.Fill first row:
dp[0][j] = dp[0][j-1]ifpat[j-1] == '*'.
Fill DP Table:
For each position
(i, j):If
pat[j-1] == '*': Can match empty sequencedp[i][j-1]or match one/more charactersdp[i-1][j].If
pat[j-1] == '?'or characters match: Take diagonal valuedp[i-1][j-1].Otherwise: No match,
dp[i][j] = false.
Return Result:
Final answer is
dp[n][m]indicating if entire text matches entire pattern.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n*m), where n is the length of the text and m is the length of the pattern. We fill each cell of the DP table exactly once.
Expected Auxiliary Space Complexity: O(n*m), as we use a 2D DP table to store intermediate matching results for all subproblems.
π§βπ» Code (C++)
class Solution {
public:
bool wildCard(string &txt, string &pat) {
int n = txt.size(), m = pat.size();
vector<vector<bool>> dp(n + 1, vector<bool>(m + 1, false));
dp[0][0] = true;
for (int j = 1; j <= m; j++)
if (pat[j - 1] == '*') dp[0][j] = dp[0][j - 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (pat[j - 1] == '*')
dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
else if (pat[j - 1] == '?' || txt[i - 1] == pat[j - 1])
dp[i][j] = dp[i - 1][j - 1];
}
}
return dp[n][m];
}
};β Code (Java)
class Solution {
public boolean wildCard(String txt, String pat) {
int n = txt.length(), m = pat.length();
boolean[][] dp = new boolean[n + 1][m + 1];
dp[0][0] = true;
for (int j = 1; j <= m; j++)
if (pat.charAt(j - 1) == '*') dp[0][j] = dp[0][j - 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (pat.charAt(j - 1) == '*')
dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
else if (pat.charAt(j - 1) == '?' || txt.charAt(i - 1) == pat.charAt(j - 1))
dp[i][j] = dp[i - 1][j - 1];
}
}
return dp[n][m];
}
}π Code (Python)
class Solution:
def wildCard(self, txt, pat):
n, m = len(txt), len(pat)
dp = [[False] * (m + 1) for _ in range(n + 1)]
dp[0][0] = True
for j in range(1, m + 1):
if pat[j - 1] == '*': dp[0][j] = dp[0][j - 1]
for i in range(1, n + 1):
for j in range(1, m + 1):
if pat[j - 1] == '*':
dp[i][j] = dp[i][j - 1] or dp[i - 1][j]
elif pat[j - 1] == '?' or txt[i - 1] == pat[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
return dp[n][m]π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated