#K12326. Wildcard Pattern Matching
Wildcard Pattern Matching
Wildcard Pattern Matching
You are given two strings, s
and p
. The string p
is a pattern that may include wildcard characters:
?
matches any single character.*
matches any sequence of characters (including the empty sequence).
Your task is to determine if the entire string s
matches the pattern p
.
Note: The matching should cover the entire input string (not partial).
Examples:
- Input: s = "aa", p = "a" → Output: False
- Input: s = "aa", p = "*" → Output: True
- Input: s = "cb", p = "?a" → Output: False
- Input: s = "adceb", p = "*a*b" → Output: True
- Input: s = "acdcb", p = "a*c?b" → Output: False
The problem can be formally described with the following recurrence in latex format:
\(dp[i][j] = \begin{cases} dp[i][j-1] \quad &\text{if } p_j = '*' \ dp[i-1][j-1] \quad &\text{if } p_j = '?' \text{ or } s_i = p_j \ \text{False} &\text{otherwise} \end{cases}\)
inputFormat
The input consists of two lines:
- The first line contains the string
s
. - The second line contains the pattern
p
.
Both s
and p
are non-empty strings. They may contain spaces and special characters.
outputFormat
Output a single line containing either True
or False
(without quotes) indicating whether the string s
matches the pattern p
completely.
aa
a
False