10 Regular Expression Matching

Implement regular expression matching with support for '.' and '*'.

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

The matching should cover the entire input string (not partial).

The function prototype should be: bool isMatch(const char s, const char p)

Some examples:

isMatch("aa","a") → false

isMatch("aa","aa") → true

isMatch("aaa","aa") → false

isMatch("aa", "a*") → true

isMatch("aa", ".*") → true

isMatch("ab", ".*") → true

isMatch("aab", "c*a*b") → true

1. Brute Force:

  1. use i, j to traverse the String s and String p. Seperate it to 2 situations:
    1. if p[j] is not '*', check either s[i]==p[j], or p[i]=='.', if so continue to s[i+1] and p[j+1]; otherwise, return false;
    2. if p[j] is '*', then we need to do a brute force exhaustive matching of 0,1, or more repeats of current character of p until we could not match anymore characters.

2. Dynamic Programming

  • 2-D boolean Array dp[s.length()+1][p.length()+1] to use dp[i][j] present whether string[0, i-1] matches pattern [0,j-1]
  • To initialize:
    • dp[0][0] should be true, which means empty string matches empty string.
    • dp[i][0] should be always false, when string is not empty, but the pattern is empty.
    • dp[0][j] depends on whether there are ‘’, because could match empty string, for example ‘a*’ could match an empty string
  • To get the value dp[i+1][j+1]:
    • the simplest scenario:
      • if (s[i]==p[j]|| p[j]==’.’), that means we need to check whether s[0, i-1] matches p[0, j-1], then dp[i+1][j+1] = dp[i][j]
    • the complicated scenario:
      • if(p[j]==’*’), that means we need to check the wildcard, which might match the string zero time, one time, or multiple times:
      • if p.charAt(j-1) != s.charAt(i) : dp[i][j] = dp[i][j-2] //in this case, a* only counts as empty
      • if p.charAt(i-1) == s.charAt(i) or p.charAt(i-1) == '.':
                          dp[i][j] = dp[i-1][j]    //in this case, a* counts as multiple a 
                       or dp[i][j] = dp[i][j-1]   // in this case, a* counts as single a
                       or dp[i][j] = dp[i][j-2]   // in this case, a* counts as empty
        

Code:

public static boolean isMatch(String s, String p)
{
       if(s == null || p == null) {
            return false;
        }
        boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
        dp[0][0] = true;
        // no need to initialize state[i][0] as false
        // initialize state[0][j]
        for (int j = 1; j < dp[0].length; j++) {
            if (p.charAt(j - 1) == '*') {
                if (dp[0][j - 1] || (j > 1 && dp[0][j - 2])) {
                    dp[0][j] = true;
                }
            } 
        }
        for (int i = 1; i < dp.length; i++) {
            for (int j = 1; j < dp[0].length; j++) {

                if (s.charAt(i-1) == p.charAt(j-1) || p.charAt(j-1) == '.') {
                    dp[i][j] = dp[i-1][j-1];
                }

                if (p.charAt(j-1) == '*') {
                    if (s.charAt(i-1) != p.charAt(j-2) && p.charAt(j-2) != '.') {
                        dp[i][j] = dp[i][j - 2];
                    } else {
                        dp[i][j] = dp[i - 1][j] || dp[i][j - 1] || dp[i][j - 2];
                    }
                }
            }
        }
        return dp[s.length()][p.length()];
}

results matching ""

    No results matching ""