Regular Expression Matching - Problem

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).

Input & Output

Example 1 — Basic Star Pattern
$ Input: s = "aa", p = "a*"
Output: true
💡 Note: The pattern 'a*' means zero or more 'a' characters. Since the string contains exactly two 'a's, the pattern matches.
Example 2 — Dot Wildcard
$ Input: s = "ab", p = ".*"
Output: true
💡 Note: The pattern '.*' means zero or more of any character. This can match any string, including 'ab'.
Example 3 — No Match
$ Input: s = "mississippi", p = "mis*is*p*."
Output: false
💡 Note: The pattern expects one more character at the end (the '.'), but the string ends after 'i'.

Constraints

  • 1 ≤ s.length ≤ 20
  • 1 ≤ p.length ≤ 30
  • s contains only lowercase English letters
  • p contains only lowercase English letters, '.', and '*'
  • '*' always appears after a preceding element

Visualization

Tap to expand
Regular Expression MatchingString s"aa"Pattern p"a*"ResulttrueMatch each character:aamatchesa*char 1char 2match a0+ timesSpecial Characters: '.' matches any char, '*' matches 0+ of preceding
Understanding the Visualization
1
Input
String s="aa" and pattern p="a*"
2
Process
Check if pattern can match entire string
3
Output
Return true if complete match found
Key Takeaway
🎯 Key Insight: Use dynamic programming to handle overlapping subproblems when matching patterns with wildcards
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28
280.5K Views
High Frequency
~35 min Avg. Time
8.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen