Regular Expression Matching
Detailed guide and Python implementation for the 'Regular Expression Matching' problem.
1. Concept Overview
The 'Regular Expression Matching' problem is a key challenge in the 2D DP section.
This implementation focuses on medium-level logic in Python.
We prioritize technical accuracy and code readability in our provided solutions.
2. Real-World Applications
3. Visual Intuition
Visualizing the logic flow for Regular Expression Matching.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Regular Expression Matching carefully.
2. Formulate brute force
Draft a simple iterative solution.
3. Identify inefficiency
Look for redundant calculations.
4. Optimize search path
Use hashing or sorting to speed up the process.
5. Final Implementation
Clean up the code for production standards.
Problem Statement
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.
- •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
s = "aa", p = "a"
False
"a" does not match the entire string "aa".
s = "aa", p = "a*"
True
'*' repeats the preceding 'a' once to match "aa".
s = "ab", p = ".*"
True
".*" matches zero or more of any character.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Interview Insights & Variations
Complexity Analysis Breakdown
Why Time: Directly evaluates all possibilities.
Why Space: Uses standard local memory.
Why Time: Optimized paths reduce total operations.
Why Space: May trade memory for speed.
Optimized Solution Python Code
Optimized Solution Python Code
def is_match_opt(s, p):
cache = {}
def dfs(i, j):
if (i, j) in cache: return cache[(i, j)]
if i >= len(s) and j >= len(p): return True
if j >= len(p): return False
match = i < len(s) and (s[i] == p[j] or p[j] == ".")
if (j + 1) < len(p) and p[j + 1] == "*":
cache[(i, j)] = (dfs(i, j + 2) or (match and dfs(i + 1, j)))
return cache[(i, j)]
if match:
cache[(i, j)] = dfs(i + 1, j + 1); return cache[(i, j)]
cache[(i, j)] = False; return False
return dfs(0, 0)Brute Force Code (Spoiler Guarded)
Brute Force Code (Spoiler Guarded)
def is_match_brute(s, p):
if not p: return not s
first = bool(s) and p[0] in [s[0], '.']
if len(p) >= 2 and p[1] == '*':
return is_match_brute(s, p[2:]) or (first and is_match_brute(s[1:], p))
else:
return first and is_match_brute(s[1:], p[1:])Algorithm Pattern Checklist
When dealing with 2D DP data patterns.
Core Prerequisites
Revision Key Notes
Common Mistakes & Pitfalls
Related Questions
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Use Regular Expressions
Master Python regular expressions using the built-in re module. Learn string matching, pattern searching, finding all occurrences, and text replacement.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.