正则表达式匹配

正则表达式匹配

class Solution {
    public boolean isMatch(String s, String p){
        int n = s.length(), m = p.length();
        boolean[][] f = new boolean[n + 1][m + 1];
        s = " " + s;
        p = " " + p;
        f[0][0] = true;
        for (int i = 0; i <= n; i ++)
            for (int j = 1; j <= m; j ++) {
                if ((j + 1 <= m) && p.charAt(j + 1) == '*') continue;
                if (p.charAt(j) == '*') {                
                    f[i][j] = f[i][j - 2];
                    if (i > 0 && (p.charAt(j - 1) == '.' || s.charAt(i) == p.charAt(j - 1))) f[i][j] |= f[i - 1][j];
                }
                else {
                    if (i > 0 && (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.')) f[i][j] = f[i - 1][j - 1];
                }
            }
        // System.out.println(Arrays.deepToString(f));
        return f[n][m];
    }
}
上一篇:125. 验证回文串


下一篇:基本计算器Ⅱ