Regular Expression Matching Solution for Accenture

Company: Accenture · Difficulty: Easy · Topics: String, Dynamic Programming, Recursion

Problem Statement

Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where:

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: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".

Example 3:

Input: s = "ab", p = ".*"

Output: true

Explanation: ".*" means "zero or more (*) of any character (.)".

Intuition

The key insight is that this is a dynamic programming problem where we need to match the string against the pattern character by character, handling the special cases of '.' and '*' recursively.

Approach

Approach 1: Dynamic Programming (Optimal)

1. Create a 2D DP table

2. Base cases: empty string and empty pattern

3. Handle '.' and '*' cases separately

4. Fill the DP table bottom-up

Approach 2: Recursion with Memoization

1. Use recursive function with memoization

2. Handle base cases first

3. Check for '*' and '.' patterns

4. Memoize results to avoid recalculation

Approach 3: Backtracking

1. Use backtracking approach

2. Try different combinations of '*' matches

3. Return true if any combination works

Dry Run

Let's trace through Example 2: s = "aa", p = "a*"

DP Approach:

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Dynamic Programming (Optimal)

Time Complexity: O(m*n) - Fill DP table

Space Complexity: O(m*n) - DP table space

Solution 2: Recursion with Memoization

Time Complexity: O(m*n) - With memoization

Space Complexity: O(m*n) - Recursion stack + memoization

Solution 3: Backtracking

Time Complexity: O((m+n)*2^(m+n)) - Exponential

Space Complexity: O(m+n) - Recursion stack

More Accenture coding questions