zoukankan      html  css  js  c++  java
  • LeetCode解题报告—— Regular Expression Matching

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

    分析:没什么思路,还是看了一下discussion,解法是用dp,感觉hard的题目大部分解法是 DP,BFS和DFS这几种。这题使用二维的dp数组来解决,有点类似求公共最长字串的那个dp。本题要明白的是,字符串和模式串的匹配是从左往右进行,可以分解为重复的子问题,比如例子5中的 s 就可以根据索引来分解为 m, mi, mis, miss.......等字串,然后p也可以分局索引来分解成字串,这样构成了dp数组dp[i][j],表示从 0~i 的子串和从0~j的子模式串是否匹配,那么么最后要求的结果是 dp[s.length()][p.length()]。

    dp的要点之一是从低向上计算,后面的计算会用到前面的计算结果,所以理清楚前后的计算关系是很重要的,对于这题来说(考虑到从左往右匹配过程的一般位置 i, j),情况如下

    1, If p.charAt(j) == s.charAt(i) :  dp[i][j] = dp[i-1][j-1];
    2, If p.charAt(j) == '.' : dp[i][j] = dp[i-1][j-1];
    3, If p.charAt(j) == '*': 
       here are two sub conditions:
                   1   if p.charAt(j-1) != s.charAt(i) : dp[i][j] = dp[i][j-2]  //in this case, a* only counts as empty
                   2   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

    代码:

    class Solution {
        public 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;
            for(int i=0;i<p.length();i++){
                if(p.charAt(i)=='*' && dp[0][i-1]==true){ // 当i=0时,p.charAt(0)不会是*,当i=1时,比如p为a*,正好匹配s为空时的情况。dp[0][1]一定是false
                    dp[0][i+1]=true;
                }
            }
            
            for(int i=0;i<s.length();i++){
                for(int j=0;j<p.length();j++){
                    if(s.charAt(i)==p.charAt(j) || p.charAt(j)=='.'){
                        dp[i+1][j+1]=dp[i][j];
                    }else if(p.charAt(j)=='*'){
                        if(s.charAt(i)!=p.charAt(j-1) && p.charAt(j-1)!='.'){
                            dp[i+1][j+1]=dp[i+1][j-1];
                        }else{
                            dp[i+1][j+1]= dp[i][j+1] || dp[i+1][j-1]; // a* 匹配掉 s 中的一个a,也可以不匹配 
                        }
                    }
                }
            }
            return dp[s.length()][p.length()];
        }
    }

    对于dp,要注意的的是对于每个 dp[i][j] 都应该将其作为一个单独问题考虑才对,不要像递归或者回溯那样考虑其在整个问题中和其它子问题的前后依赖关系或计算过程等,这样有助于理清思路。

    还要一个是上面的标记的注意点,如果 p.charAt(j-1) != s.charAt(i) && p.charAt(j-1) != '.',这种条件下 a* 这样的模式不能匹配s.charAt(i),那么dp[i+1][j+1] 的选择只能是dp[i+1][j-1],也就是 a 出现的次数是0个,那么i+1 索引是不动的,而模式串要舍弃掉后两位。如果能匹配上,注意的是,这时候有2个选择,可以匹配一个,也可以选择不匹配。

  • 相关阅读:
    js、css等文件引入空白问题
    Vue 组件 data为什么是函数
    安装Node,创建vue项目,运行及打包
    JQuery移除事件
    百度地图定位
    移动端导航过多,点击导航左右滚动回弹
    移动端开发模板
    移动端左右滑动导航
    使用‘圣杯布局’、‘双飞翼布局’来解释自适应的三栏水平布局
    css实现三角箭头
  • 原文地址:https://www.cnblogs.com/f91og/p/9379990.html
Copyright © 2011-2022 走看看