#K49867. Wildcard Pattern Matching
Wildcard Pattern Matching
Wildcard Pattern Matching
Given two strings, s1
and s2
, determine if s2
matches s1
using wildcard pattern matching. The pattern string s2
may contain the wildcard characters *
and ?
. The *
character matches any sequence (including the empty sequence) of characters, while the ?
character matches any single character.
Your task is to implement this matching logic and output True
if the pattern matches the string, and False
otherwise.
The matching should be done using a dynamic programming approach, for instance using a DP table where dp[i][j] is True
if the first i characters of the pattern match the first j characters of the string.
The formulas involved can be summarized as follows:
- Initialization: \(dp[0][0]=True\)
- If \(s2[i-1] = '*'\), then \(dp[i][j] = dp[i-1][j] \lor dp[i][j-1]\)
- If \(s2[i-1] = '?'\) or \(s2[i-1] = s1[j-1]\), then \(dp[i][j] = dp[i-1][j-1]\)
Note: The input is provided via stdin and the result should be printed to stdout.
inputFormat
The input consists of two lines:
- The first line contains the string
s1
. - The second line contains the pattern string
s2
, which may include the wildcard characters '*' and '?'.
outputFormat
Print a single line to stdout
with either True
or False
depending on whether s2
matches s1
according to the wildcard rules.
aa
a
False