Top 150 InterviewMedium

Regular Expression Matching

Detailed guide and Python implementation for the 'Regular Expression Matching' problem.

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?
Consider using 2D DP-specific data structures like sets or heaps.
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.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.