Skip to the content.

LeetCode 10. Regular Expression Matching

Problem Statement

Given an input string s and a pattern p, this algorithm implements regular expression matching with support for two special characters:

Test Cases

Example 1:

Example 2:

Example 3:

Solution

The solution provides two implementations of regular expression matching:

Dynamic Programming (DP)

The second solution uses dynamic programming to solve the problem. It creates a DP matrix dp to keep track of matching characters. It iterates through both the string s and pattern p, considering various cases, such as matching characters or wildcard ‘*’, and updates the DP matrix accordingly. The final result is stored in dp[m][n], where m and n are the lengths of strings s and p, respectively.

Complexity