Skip to content

Regular Expression Match

Logical Breakdown

  • [ ] Subproblem Identification:
  • [ ] Optimal Substructure:
  • [ ] Constraint Handling: (e.g., Modulo \(10^9 + 7\))
  • [ ] Optimization: (Matrix Exponentiation if 'A' is huge)

Mathematical Rigor

State Definition

Let \(dp[i]\) be the state for the \(i\)-th subproblem.

Recurrence Relation

\[ dp[i] = ... \]

Visualization

graph TD A["Current State"] --> B["Previous State 1"] A --> C["Previous State 2"]

Complexity Analysis

Approach Time Complexity Space Complexity
Iterative DP \(O(N)\) \(O(1)\)

Code Reference

package com.dsa.dynamic_programming;

import java.util.*;

/ * Problem: Regular Expression Match * Group: 11. Dynamic Programming */ / * ============================================================================================ * PROBLEM: Regular Expression Match (Wildcard Matching) * ============================================================================================ * * --------------------- * 1. Problem Description * --------------------- * Implement matching for: * '?' : Matches any single character. * '' : Matches any sequence of characters (including empty). * * --------------------- * 2. Logical Breakdown * --------------------- * Let dp[i][j] = boolean (Is match possible for String S[0..i-1] and Pattern P[0..j-1]?) * * Logic: * 1. If P[j-1] == '?' or S[i-1] == P[j-1]: * Match single char. * dp[i][j] = dp[i-1][j-1] * * 2. If P[j-1] == '': * Two choices: * a) '' matches Empty Sequence: We ignore '' and look at P[0..j-2]. * -> dp[i][j-1] * b) '' matches Current Char S[i-1] (and possibly more before it): * -> dp[i-1][j] * dp[i][j] = dp[i][j-1] || dp[i-1][j] * * Base Cases: * dp[0][0] = true. * dp[i][0] = false (Pattern empty, string not). * dp[0][j] = depends (True only if P[0..j-1] is all '') * * ============================================================================================ */ public class RegularExpressionMatch { public int solve(int A) { // TODO: Implement solution return 0; }

public int isMatch(String aa, String s) {
    return 1;
}

}