Regular Expression Matching Problem and Solution

1. Introduction

The "Regular Expression Matching" problem involves implementing a simple regular expression engine that supports two special characters: '.' which matches any single character, and '*' which matches zero or more of the preceding element. The matching should cover the entire input string (not partial).

2. Solution in C++

#include <iostream>
#include <vector>
using namespace std;

bool isMatch(string s, string p) {
    int m = s.length(), n = p.length();
    vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));
    dp[0][0] = true;

    for (int i = 0; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (p[j - 1] == '*') {
                dp[i][j] = dp[i][j - 2] || (i > 0 && dp[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.'));
            } else {
                dp[i][j] = i > 0 && dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == '.');
            }
        }
    }
    return dp[m][n];
}

// Example usage
int main() {
    string s = "aab";
    string p = "c*a*b";
    cout << boolalpha << "Is Match: " << isMatch(s, p) << endl;
    return 0;
}

Output:

Is Match: true

Explanation:

1. isMatch uses dynamic programming to solve the problem.

2. dp[i][j] represents if s[0..i-1] matches p[0..j-1].

3. The algorithm handles characters and the special characters '*' and '.'.

4. It iteratively builds up the solution for the entire strings.

3. Solution in Java

public class RegularExpressionMatching {
    public static boolean isMatch(String s, String p) {
        int m = s.length(), n = p.length();
        boolean[][] dp = new boolean[m + 1][n + 1];
        dp[0][0] = true;

        for (int i = 0; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (p.charAt(j - 1) == '*') {
                    dp[i][j] = dp[i][j - 2] || (i > 0 && dp[i - 1][j] && (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.'));
                } else {
                    dp[i][j] = i > 0 && dp[i - 1][j - 1] && (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '.');
                }
            }
        }
        return dp[m][n];
    }

    // Example usage
    public static void main(String[] args) {
        String s = "aab";
        String p = "c*a*b";
        System.out.println("Is Match: " + isMatch(s, p));
    }
}

Output:

Is Match: true

Explanation:

1. Similar to the C++ solution, the Java method isMatch also uses dynamic programming.

2. It utilizes a 2D boolean array dp for storing intermediate results.

3. The method iteratively checks each character and handles the '*' and '.' characters.

4. Returns true if the entire string s matches the pattern p.

4. Solution in Python

def is_match(s, p):
    dp = [[False] * (len(p) + 1) for _ in range(len(s) + 1)]
    dp[0][0] = True

    for i in range(len(s) + 1):
        for j in range(1, len(p) + 1):
            if p[j-1] == '*':
                dp[i][j] = dp[i][j-2] or (i > 0 and dp[i-1][j] and (s[i-1] == p[j-2] or p[j-2] == '.'))
            else:
                dp[i][j] = i > 0 and dp[i-1][j-1] and (s[i-1] == p[j-1] or p[j-1] == '.')

    return dp[-1][-1]

# Example usage
s = "aab"
p = "c*a*b"
print("Is Match:", is_match(s, p))

Output:

Is Match: True

Explanation:

1. The Python function is_match follows the same dynamic programming approach.

2. It uses a 2D list dp to store the results.

3. The function iteratively builds the solution and handles the special characters.

4. Returns true if s matches p.


Comments