52. 正则表达式匹配

别来无恙 提交于 2019-12-27 17:31:05

题目描述

1. 递归

代码实现

/**
 * @Classname Solution
 * @Description 正则表达式匹配
 * @Date 2019/12/24 10:45
 * @Author SonnSei
 */
public class Solution {
    public static boolean match(char[] str, char[] pattern) {
        if (str == null || pattern == null) return false;
        return match(str, 0, pattern, 0);
    }

    private static boolean match(char[] str, int sIndex, char[] pattern, int pIndex) {
        if(pIndex == pattern.length)return sIndex==str.length;
        boolean firstMatch = sIndex < str.length && (str[sIndex] == pattern[pIndex] ||pattern[pIndex]=='.');
        // 发现通配符
        if (pIndex < pattern.length - 1 && pattern[pIndex + 1] == '*') {
            return match(str, sIndex, pattern, pIndex + 2) || (firstMatch && match(str, sIndex + 1, pattern, pIndex));
        } else {
            return firstMatch && match(str, sIndex + 1, pattern, pIndex + 1);
        }
    }
}

2. dp自顶向下备忘录

代码实现

/**
 * @Classname Solution2
 * @Description 自顶向下
 * @Date 2019/12/24 14:30
 * @Author SonnSei
 */
public class Solution2 {
    public static boolean match(char[] str, char[] pattern) {
        if (str == null || pattern == null) return false;
        // 0表示未探查,1表示true,2表示false
        // 为什么数组长度要是length+1? 因为length可能为0,但是我们的数组是必须要初始化的
        // 大多数情况下数组空间有可能会有冗余
        Boolean[][] dp = new Boolean[str.length + 1][pattern.length + 1];
        return match(str, 0, pattern, 0, dp);
    }


    private static boolean match(char[] str, int sIndex, char[] pattern, int pIndex, Boolean[][] dp) {
        if (pIndex == pattern.length) return sIndex == str.length;
        Boolean ans;
        if (dp[sIndex][pIndex] != null) {
            ans = dp[sIndex][pIndex];
        } else {
            boolean firstMatch = sIndex < str.length &&
                    (str[sIndex] == pattern[pIndex] || pattern[pIndex] == '.');
            // 发现通配符
            if (pIndex < pattern.length - 1 && pattern[pIndex + 1] == '*') {
                ans = match(str, sIndex, pattern, pIndex + 2, dp) || (firstMatch && match(str, sIndex + 1, pattern, pIndex, dp));
            } else {
                ans = firstMatch && match(str, sIndex + 1, pattern, pIndex + 1, dp);
            }
        }
        dp[sIndex][pIndex] = ans;
        return ans;
    }
}

复杂度分析

时间复杂度:O(sLen×pLen){O(sLen\times pLen)}
空间复杂度:O(sLen×pLen){O(sLen\times pLen)}

3. dp自底向上

代码实现


/**
 * @Classname Solution3
 * @Description 自底向上
 * @Date 2019/12/24 14:56
 * @Author SonnSei
 */
public class Solution3 {
    public static boolean isMatch(String text, String pattern) {
        boolean[][] dp = new boolean[text.length() + 1][pattern.length() + 1];
        dp[text.length()][pattern.length()] = true;

        for (int i = text.length(); i >= 0; i--) {
            for (int j = pattern.length() - 1; j >= 0; j--) {
                boolean first_match = (i < text.length() &&
                        (pattern.charAt(j) == text.charAt(i) ||
                                pattern.charAt(j) == '.'));
                if (j + 1 < pattern.length() && pattern.charAt(j + 1) == '*') {
                    dp[i][j] = dp[i][j + 2] || first_match && dp[i + 1][j];
                } else {
                    dp[i][j] = first_match && dp[i + 1][j + 1];
                }
            }
        }
        return dp[0][0];
    }

    public boolean match(char[] str, char[] pattern) {
        if(str == null || pattern == null)return false;
        boolean[][] dp = new boolean[str.length + 1][pattern.length + 1];
        dp[str.length][pattern.length] = true;
        for (int i = str.length; i >=0 ; i--) {
            for (int j = pattern.length-1; j >=0 ; j--) {
                boolean firstMatch = i < str.length &&
                        (pattern[j] == str[i]||pattern[j]=='.');
                if (j + 1 < pattern.length && pattern[j + 1] == '*') {
                    dp[i][j] = dp[i][j + 2] || firstMatch && dp[i + 1][j];
                } else {
                    dp[i][j] = firstMatch && dp[i+ 1][j + 1];
                }
            }
        }
        return dp[0][0];
    }
}

复杂度分析

时间复杂度:O(sLen×pLen){O(sLen\times pLen)}
空间复杂度:O(sLen×pLen){O(sLen\times pLen)}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!