1. 程式人生 > >DP動態規劃專題十:LeetCode 10. Regular Expression Matching

DP動態規劃專題十:LeetCode 10. Regular Expression Matching

LeetCode 10. Regular Expression Matching

Given an input string (s) and a pattern §, 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).

Note:

s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like . or *.

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 precedeng 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 (.)".
Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
Example 5:
Input:
s = "mississippi"
p = "mis*is*p*."
Output: false

版本一:iteration

    public boolean isMatch(String s, String p) {
        boolean[][] dp = new boolean[s.length()+1][p.length()+1];
        dp[s.length()][p.length()] = true;
        for (int i = p.length() - 2; i >= 0; i--) dp[s.length()][i] = (p.charAt(i+1) == '*' && dp[s.length()][i+2]) ? true: false;
        for (int i = s.length() - 1; i >= 0; i--) {
            for (int j = p.length() - 1; j >= 0; j--) {
                if (j < p.length() - 1 && p.charAt(j+1) == '*') {
                    dp[i][j] = dp[i][j+2];
                    if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.') {
                        dp[i][j] = dp[i][j] || dp[i+1][j];
                    }
                } else if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.') {
                    dp[i][j] = dp[i+1][j+1];
                } else {
                    continue;
                }
            }
        }
        return dp[0][0];
    }

版本二:recursion

    public boolean isMatch(String s, String p) {
        boolean[][] dp = new boolean[s.length()][p.length()];
        return recursion(s, p, dp, 0, 0);
    }
    private boolean recursion(String s, String p, boolean[][] dp, int i, int j) {
        //base case
        boolean res = false;
        if (i == s.length() && j == p.length()) return true;
        else if (j == p.length()) return false;
        else if (i == s.length()) return (j < p.length() - 1 && p.charAt(j+1) == '*' && recursion(s, p, dp, i, j + 2));
        else if (dp[i][j] == true) return false;
        else {
            if (j < p.length() - 1 && p.charAt(j+1) == '*') {
                if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.') {
                    res = res || recursion(s, p, dp, i + 1, j) || recursion(s, p, dp, i, j + 2);
                } else {
                    res = res || recursion(s, p, dp, i, j + 2);
                }
            } else if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.') {
                res = res || recursion(s, p, dp, i + 1, j + 1);
            } else {}
            
        }
        if (res == false) dp[i][j] = true;
        //recursion
        return res;
    }