Solving Regular Expression Matching with Dynamic programming in Java

2017-08-28

Here is the description of Regular Expression Matching from Leetcode:

'.' 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
And the default code is:
public boolean isMatch(String s, String p){
}

Using Dynamic programming to solve it

Firstly, we need to create a boolean array.
boolean[][] map = new boolean[s.length+1][p.length+1] (we called it map). Because the map[0][0] should be true, and it presents that “” is matched with “”.
Secondly, let’s think about the conditions:

  1. s: abc — p: abc or p: a.c (In this case, s.charAt(i-1) == p.charAt(j-1) || p.charAt(j-1) == ‘.’), map[i][j] = map[i-1][j-1] Remember that map starts with “”, so to match it, map[i] -> s.charAt(i-1)
  2. p.charAt(j-1) == '*'
    This is condition, things mean to be more complex. We will have two sub condition:

    • We assign map[i][j] = map[i][j-2], which is to skip '*'. Because '*' matches zero or more of the preceding element.

    • if(s.charAt(i-1) == p.charAt(j-2) || p.charAt(j-2) == ‘.’)


    So when p.charAt(j-1) == ‘*’, the boolean value could be map[i][j-2] || map[i-1][j].

Sepcial case to think about

When the array is "a*b*".
At first, we need to consider these special condition.
p.charAt(i-1) == ‘*’, map[0][i] = map[0][i-2].

Java Code:


public boolean isMatch(String s, String p) {
        boolean[][] map = new boolean[s.length()+1][p.length()+1];
        map[0][0] = true;
        for(int i = 1; i < p.length()+1; i++){
            if(p.charAt(i-1) == '*'){
                map[0][i] = map[0][i-2];
            }
        }

        for(int i = 1; i < s.length()+1;i++){
            for(int j = 1; j < p.length()+1;j++){
                if(s.charAt(i-1) == p.charAt(j-1) || p.charAt(j-1) == '.'){
                    map[i][j] = map[i-1][j-1];
                }
                else if(p.charAt(j-1) == '*'){
                    map[i][j] = map[i][j-2];
                    if(p.charAt(j-2) == '.' || p.charAt(j-2) == s.charAt(i-1)){
                        map[i][j] = map[i-1][j] || map[i][j];
                    }
                }
                else{
                    map[i][j] = false;
                }
            }
        }
        return map[s.length()][p.length()];
    }


Comments: