Back to Practice Dashboard
Top 150 InterviewMedium
Regular Expression Matching
Learn how to solve the 'Regular Expression Matching' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Medium
Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where:
- '.' Matches any single character.
- '*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
Write a function isMatch(s: str, p: str) -> bool.
Constraints
- •1 <= len(s) <= 20
- •1 <= len(p) <= 20
- •s contains only lowercase English letters.
- •p contains only lowercase English letters, '.', and '*'.
- •It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.
Examples
Example 1
Input
s = "aa", p = "a"
Output
False
Explanation
"a" does not match the entire string "aa".
Example 2
Input
s = "aa", p = "a*"
Output
True
Explanation
'*' repeats the preceding 'a' once to match "aa".
Example 3
Input
s = "ab", p = ".*"
Output
True
Explanation
".*" matches zero or more of any character.
Need a Hint?
Define subproblem states, establish the recurrence relation, and use memoization (top-down) or tabulation (bottom-up).
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.