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个选择,可以匹配一个,也可以选择不匹配。

LeetCode解题报告—— Regular Expression Matching的更多相关文章

  1. 【一天一道LeetCode】#10. Regular Expression Matching

    一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...

  2. 【leetcode】10.Regular Expression Matching

    题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

  3. [Leetcode][Python][DP]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...

  4. 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. leetcode problem 10 Regular Expression Matching(动态规划)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. LeetCode OJ:Regular Expression Matching(正则表达式匹配)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  7. 【LeetCode】010. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. 【LeetCode】10.Regular Expression Matching(dp)

    [题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...

  9. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

随机推荐

  1. IDEA中使用Docker: 图形化 or 命令行 ,你更稀罕那个??

    Docker简介: Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化. 容器是完全使用沙箱机 ...

  2. 认识MEAN开发框架[转]

    MEAN是一个Javascript平台的现代Web开发框架总称,它是MongoDB + Express +AngularJS + NodeJS 四个框架的第一个字母组合. MongoDB是一个使用JS ...

  3. cmder 添加到右键菜单

    管理员权限打开cmde 输入: cmder /register all 回车,OK

  4. linux添加vim编辑器和一些用法

    vim.tar文件在自己的百度云盘里面,linux目录下 上传vim.tar文件,解压 vim编辑文件的一些快捷方式: n+t打开文件所在目录,显示在左侧 ctrl+w+l 切换到右边文件ctrl+w ...

  5. 牛客练习赛42 出题的诀窍(数学+hash)

    出题的诀窍 题目链接:https://ac.nowcoder.com/acm/contest/393/C 题解: 由于他是在每一行选取一个元素,然后纵向来比较,这里行的顺序是不会影响的,所以我们将每一 ...

  6. POJ3186 DP

    Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5753   Accepted: 29 ...

  7. 主机不能访问虚拟机CentOS中的站点

    主机能ping通虚拟机 虚拟机也能ping通主机 主机不能telenet通虚拟机 原因:虚拟机开启了防火墙, 解决办法:关闭虚拟机防火墙. Centos 7 firewall 命令: 查看已经开放的端 ...

  8. 51Nod 1305 Pairwise Sum and Divide | 思维 数学

    Output 输出fun(A)的计算结果. Input示例 3 1 4 1 Output示例 4 first try: #include "bits/stdc++.h" using ...

  9. 利用forEach循环Dom元素…

    大家都知道forEach是循环数组用的,而且很方便,可以丢掉for循环了,但是它不能循环Dom元素.其实我们可以利用call来完成forEach循环Dom; 假设有这样的HTML结构: <ul ...

  10. 【洛谷 P4180】【模板】严格次小生成树[BJWC2010](倍增)

    题目链接 题意如题. 这题作为我们KS图论的T4,我直接打了个很暴力的暴力,骗了20分.. 当然,我们KS里的数据范围远不及这题. 这题我debug了整整一个晚上还没debug出来,第二天早上眼前一亮 ...