101 Logo
onenoughtone

Problem Statement

Regular Expression Matcher

You're a software engineer working on a text processing application that needs to search through large documents for specific patterns. Your task is to implement a simplified regular expression matcher that supports two special characters:

  • . (dot) - Matches any single character
  • * (star) - Matches zero or more of the preceding element

For example, the pattern "a*" means "zero or more 'a' characters", and ".*" means "zero or more of any character".

Your function should determine if a given text string matches a given pattern. The matching should cover the entire text string, not just a substring.

Examples

Example 1:

Input: text = "aab", pattern = "c*a*b"
Output: true
Explanation: The pattern matches because 'c*' can match zero 'c's, 'a*' can match two 'a's, and 'b' matches the last 'b' in the text.

Example 2:

Input: text = "mississippi", pattern = "mis*is*p*."
Output: false
Explanation: The pattern cannot match the entire text. It can match up to 'mississip', but the final 'p' in the text doesn't match the '.' in the pattern.

Example 3:

Input: text = "ab", pattern = ".*"
Output: true
Explanation: The '.*' pattern means 'zero or more of any character', which can match the entire 'ab' string.

Constraints

  • The text and pattern strings consist only of lowercase English letters and the special characters '.' and '*'.
  • The length of the text string is between 0 and 20 characters.
  • The length of the pattern string is between 0 and 30 characters.
  • The '*' character will never appear as the first character in the pattern.
  • The '*' character will not appear consecutively (e.g., 'a**' is not a valid pattern).

Problem Breakdown

To solve this problem, we need to:

  1. The '*' character makes this problem challenging because it can match zero characters, which introduces many possible matching scenarios.
  2. This problem can be solved using dynamic programming by breaking it down into smaller subproblems.
  3. A recursive approach with memoization can also be effective for this problem.
  4. The key insight is to handle the '*' character separately from other characters due to its special behavior.
ProblemSolutionCode
101 Logo
onenoughtone