Problem 199
Regular Expression Matching
Hard · 30 pts · String, Dynamic Programming, Recursion
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 '*' means zero or more copies of the element right before it. Every '*' follows a letter or a '.'. For example, "a*" matches "", "a" and "aaa", and ".*" matches any string. Let dp[i][j] say whether the first i characters of S match the first j characters of P. A '*' either uses zero copies (skip the pair, dp[i][j - 2]) or one more copy when the preceding element matches S[i - 1] (dp[i - 1][j]).
Input
Two lines: string S (lowercase letters) and pattern P (lowercase letters, '.' and '*').
Output
Print YES if P matches all of S, otherwise NO.
Constraints
1 <= |S| <= 20; 1 <= |P| <= 20
Example input
aa a
Example output
NO
Explanation
The pattern "a" matches exactly one 'a', but the string "aa" has two. The pattern does not cover the whole string, so the answer is NO.