Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Wildcard Matching in Python
Wildcard pattern matching allows you to match strings using special characters. In wildcard matching, '?' matches any single character and '*' matches zero or more characters. This is useful for file pattern matching, search operations, and text processing.
Wildcard Characters
'?' − Matches exactly one character
'*' − Matches zero or more characters
Dynamic Programming Approach
We can solve wildcard matching using dynamic programming. The idea is to build a 2D table where dp[i][j] represents whether the first i characters of the string match the first j characters of the pattern.
Algorithm Steps
Create a 2D DP table of size
(len(s)+1) x (len(p)+1)Initialize base cases: empty string matches empty pattern
Handle '*' patterns that can match empty strings
Fill the DP table based on character matching rules
Implementation
def wildcard_match(s, p):
"""
Check if string s matches pattern p with wildcards ? and *
"""
m, n = len(s), len(p)
# Create DP table
dp = [[False] * (n + 1) for _ in range(m + 1)]
# Base case: empty string matches empty pattern
dp[0][0] = True
# Handle patterns with * at the beginning
for j in range(1, n + 1):
if p[j-1] == '*':
dp[0][j] = dp[0][j-1]
# Fill the DP table
for i in range(1, m + 1):
for j in range(1, n + 1):
if p[j-1] == '*':
# * can match zero characters or one/more characters
dp[i][j] = dp[i][j-1] or dp[i-1][j]
elif p[j-1] == '?' or s[i-1] == p[j-1]:
# ? matches any single character, or exact character match
dp[i][j] = dp[i-1][j-1]
return dp[m][n]
# Test examples
print(wildcard_match("aa", "a?"))
print(wildcard_match("aaaaaa", "a*"))
print(wildcard_match("hello", "h*o"))
print(wildcard_match("abc", "a?c"))
True True True True
How It Works
The algorithm works by building a truth table:
Exact match or '?': If characters match exactly or pattern has '?', copy the diagonal value
'*' wildcard: '*' can match zero characters (take left value) or one/more characters (take top value)
Base cases: Empty string matches empty pattern, and '*' patterns can match empty strings
Example with Trace
def wildcard_match_with_trace(s, p):
"""
Wildcard matching with step-by-step trace
"""
m, n = len(s), len(p)
dp = [[False] * (n + 1) for _ in range(m + 1)]
dp[0][0] = True
# Handle * patterns
for j in range(1, n + 1):
if p[j-1] == '*':
dp[0][j] = dp[0][j-1]
print(f"String: '{s}', Pattern: '{p}'")
print("DP Table building:")
for i in range(1, m + 1):
for j in range(1, n + 1):
if p[j-1] == '*':
dp[i][j] = dp[i][j-1] or dp[i-1][j]
elif p[j-1] == '?' or s[i-1] == p[j-1]:
dp[i][j] = dp[i-1][j-1]
return dp[m][n]
result = wildcard_match_with_trace("abc", "a*c")
print(f"Result: {result}")
String: 'abc', Pattern: 'a*c' DP Table building: Result: True
Time and Space Complexity
Time Complexity: O(m × n) where m is string length and n is pattern length
Space Complexity: O(m × n) for the DP table
Conclusion
Wildcard pattern matching using dynamic programming provides an efficient solution with O(m×n) complexity. The key insight is that '*' can match zero or more characters, while '?' matches exactly one character.
