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. bzoj1854: [Scoi2010]游戏(匈牙利) / GDKOI Day2 T2(最大流)

    题目大意:有n(<=1000000)个装备,每个装备有两个属性值(<=10000),每个装备只能用一次,使用某一个值,攻击boss必须先使用属性为1的,再使用属性为2的,再使用属性为3的, ...

  2. CentOS7搭建 Hadoop + HBase + Zookeeper集群

    摘要: 本文主要介绍搭建Hadoop.HBase.Zookeeper集群环境的搭建 一.基础环境准备 1.下载安装包(均使用当前最新的稳定版本,截止至2017年05月24日) 1)jdk-8u131 ...

  3. noip模拟赛 helloworld

    分析:对于第一个点,答案为26^n - 25^n,这个很好想.另外30%的点因为n <= 5,所以可以直接暴力搜索. 数学方法不是很好处理,考虑dp,设f[i][j]为前i位匹配到危险串第j位的 ...

  4. mysql Innodb索引

    基本概念 对于mysql目前的默认存储引擎Innodb来说,索引分为2个,一个是聚集索引,一个是普通索引(也叫二级索引). 聚集索引:聚集索引的顺序和数据在磁盘的顺序一致,因此查询时使用聚集索引,效率 ...

  5. js和jq实现全选反选

    在前端中用到全选反选的案例并不少,在这里呢我就实现这个功能给大家参考参考. 这里呢就先贴上我的html和css代码 <div class="wrap"> <tab ...

  6. elasticsearch中Node的创建

    要连接到集群,首先要告诉集群:你是谁,你有什么特征.在es中体现为实例化节点,elasticsearch通过org.elasticsearch.node.NodeBuilder的build()或者no ...

  7. floyd骚操作——传递闭包

    传递闭包的含义指通过传递性推导出尽量多的元素之间的关系,而传递闭包一般都是采用floyd算法. 下面用两道题来实现传递闭包: Problem 1(POJ3660): 题目链接:http://poj.o ...

  8. 20、redis和memcached比较?

    1.Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等: 2.Redis不仅仅支持简单的k/v类型的数据,同时还提供lis ...

  9. JS的prototype和__proto__

    一.prototype和__proto__的概念 prototype是函数的一个属性(每个函数都有一 个prototype属性),这个属性是一个指针,指向一个对象.它 是显示修改对象的原型的属性. _ ...

  10. 【bug】vue-cli 3.0报错的解决办法

    先上bug图片 bug说明:初装vue_cli3.0写了个组件,运行错误,显示如图, 代码提示:[Vue warn]: You are using the runtime-only build of ...