Leetcode 10. Regular Expression Matching
Question
Given an input string (s
) and a pattern (p
), implement the regular expression matching with support for '.'
and '*'
.
The matching should cover the entire input string (not partial).
Note
s
could be empty and contains only lowercase lettersa-z
.p
could be empty and contains only lowercase lettersa-z
, and characters like.
or*
.
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Thoughts
This is a classical dynamic programming question. However, solutions for DP questions are usually not obvious. Our best strategy is to first come up with a recursive solution and then derive the DP version from it.
In order to check if s
and p
matches, we only need to check whether their first characters match (or p
has a wildcard) and then recursive run the algorithms on their substring starting at index 1. Yet, we have to be aware that Kleene stars slightly complicates the problem by adding two following cases, before we are able to recurse.
If the first character of p
is followed by a *
, then s
and p
matches if either one of the following is true: 1. s
and p
's first character matches AND s
's substring starting at index 1 also matches the original pattern p
. (keep expanding the first matching character of pattern). 2. s
and p
's first character doesn't match AND s
matches the pattern p
's substring starting at index 2. (skip the first two unmatched pattern characters).
Then we are able to arrive at our recursive algorithm as following.
Solution
Recursive algorithm:
Personally I would prefer the more clear and intuitive recursive solution. Nonetheless, DP is also very important and gradually becomes a standard in technical interview. It's also true that the more practice you have, the more familiar and intuitive DP becomes.
Complexity Analysis
Recurisve Algorithm:
TODO
Dynamic Programming Algorithm:
Time Complexity: O(n^2), where n is the length of input and patten string. We traverse through every cell of the 2 dimensional DP matrix.
Space Complexity: O(n^2). The 2 dimensional DP matrix stores n^2 intermediate results.
Extension
Mathematically prove and compare recursion and DP algorithm runtime.
Summary
Dynamic Programming solution is usually hard to think of, especially when under stress. Yet, we can solve the problem first using recursion, which is much more intuitive, and then check if we can apply dynamic programming. When constructing the DP arrays, we have to think carefully about which intermediate results to store and how can we utilize them to reduce runtime.
Reference
Last updated
Was this helpful?