Hone logo
Hone
Problems

Regular Expression Matching

Implement a function that checks if a given text string matches a simplified regular expression pattern. This is a fundamental problem in computer science with applications in text searching, parsing, and data validation.

Problem Description

You need to write a function that takes two strings as input: a text string and a pattern string. The pattern string can contain lowercase letters ('a'-'z'), the character '.', and the character '*'.

  • '.' Matches any single character.
  • '*' Matches zero or more of the preceding element.

The matching should cover the entire text string, not just a part of it.

Key Requirements:

  • The function should return true if the text matches the pattern entirely, and false otherwise.
  • The pattern string is guaranteed to be valid. This means a '*' will always have a preceding character to match.

Expected Behavior:

  • If the pattern has no special characters, it must match the text exactly.
  • '.' in the pattern can match any single character in the text.
  • '*' in the pattern can match zero or more occurrences of the character immediately preceding it.

Edge Cases to Consider:

  • Empty text string.
  • Empty pattern string.
  • Patterns containing only '' or '.' followed by ''.
  • Text and pattern of different lengths.
  • Cases where '*' matches zero occurrences.
  • Cases where '*' matches multiple occurrences.

Examples

Example 1:

Input:
text = "aa"
pattern = "a"

Output: false

Explanation: The pattern "a" only matches the first 'a' in the text, but not the entire string.

Example 2:

Input:
text = "aa"
pattern = "a*"

Output: true

Explanation: The pattern "a*" can match zero or more 'a's. In this case, it matches two 'a's.

Example 3:

Input:
text = "ab"
pattern = ".*"

Output: true

Explanation: The pattern ".*" means "zero or more of any character". This can match any string, including "ab".

Example 4:

Input:
text = "aab"
pattern = "c*a*b"

Output: true

Explanation: The pattern "c*a*b" can be interpreted as:
- 'c*' matches zero 'c's.
- 'a*' matches two 'a's.
- 'b' matches one 'b'.
This forms the text "aab".

Example 5:

Input:
text = "mississippi"
pattern = "mis*is*p*."

Output: false

Explanation: The pattern "mis*is*p*." matches up to "mississipp", but there is no character left in the pattern to match the final 'i' in the text.

Constraints

  • The length of text will be between 0 and 20 (inclusive).
  • The length of pattern will be between 0 and 30 (inclusive).
  • text will only contain lowercase English letters ('a'-'z').
  • pattern will only contain lowercase English letters ('a'-'z'), '.', or '*'.
  • The pattern is guaranteed to be valid.

Notes

  • This problem is often solved using dynamic programming or recursion with memoization.
  • Consider how to handle the '*' character: it can match zero occurrences, one occurrence, or multiple occurrences of the preceding character.
  • Think about the base cases for your recursive or DP solution. What happens when the text or pattern is empty?
Loading editor...
plaintext