HardPro challengePythonJavaScriptTypeScript
DP — Regular Expression Matching
AlgorithmsDynamic ProgrammingStrings
solve(s, p) — implement matching of the whole string s against the patternp, which supports two special characters:
.matches any single character.*matches zero or more of the preceding element (soa*matches"",
"a", "aa", ...). The pattern never starts with * and every * follows a
valid element.
Let dp[i][j] be true when the first i characters of s match the first j
characters of p. A * can either drop its element (dp[i][j-2]) or consume one
more character of s when the element matches (dp[i-1][j]).
Examples
solve("aa", "a") → false
solve("aa", "a*") → true
solve("ab", ".*") → true
solve("aab", "c*a*b") → true
solve("mississippi", "mis*is*p*.") → false
solve("", "a*") → trueSample tests
Test #1Pattern too short
Input: ["aa","a"]
Output: false
Test #2Star repeats a
Input: ["aa","a*"]
Output: true
Test #3Dot-star matches anything
Input: ["ab",".*"]
Output: true