The Regular Expression Matching problem asks you to implement pattern matching with '.' (matches any single character) and '*' (matches zero or more of the preceding element). Return whether the full string s matches the pattern p. This hard coding problem uses DP to handle all matching cases. The recursion, string, and dynamic programming interview pattern is the core.
Apple, Uber, Microsoft, Meta, Amazon, Google, Bloomberg, and many others ask this as a premier DP challenge. It tests the ability to model complex matching logic — including the tricky '*' zero-or-more case — as a clean DP recurrence.
2D DP. dp[i][j] = whether s[0..i-1] matches p[0..j-1]. Base: dp[0][0]=True. dp[0][j]: true if pattern matches empty string (e.g., "ab").
Transitions:
s="aab", p="cab". dp table:
Regular Expression Matching is one of the hardest foundational DP problems. The '' case has two sub-cases: (1) use zero occurrences → dp[i][j-2]; (2) use one or more → check if preceding character matches current s[i-1], then dp[i-1][j]. Derive the recurrence on paper with small examples before coding. Practice Wildcard Matching ('?' and '') as a simpler warm-up before this problem.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Wildcard Matching | Hard | Solve | |
| Distinct Subsequences | Hard | Solve | |
| Count Palindromic Subsequences | Hard | Solve | |
| Strange Printer | Hard | Solve | |
| Shortest Common Supersequence | Hard | Solve |