Problem 297
Wildcard Matching
Hard · 30 pts · String, Dynamic Programming, Greedy
Problem
You are given a string S and a pattern P. Decide whether P matches the entire string S, not just part of it. In the pattern, a letter matches itself, '?' matches any single character, and '*' matches any sequence of characters, including the empty sequence. Let dp[i][j] say whether the first i characters of S match the first j characters of P. A '*' either matches nothing (dp[i][j - 1]) or one more character (dp[i - 1][j]). A letter or '?' must match S[i - 1] and then needs dp[i - 1][j - 1].
Input
Two lines: string S (lowercase letters) and pattern P (lowercase letters, '?' and '*').
Output
Print YES or NO.
Constraints
1 <= |S|, |P| <= 2000
Example input
aa *
Example output
YES
Explanation
The pattern "*" can match any sequence of characters, including the whole string "aa", so the answer is YES.