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 lettersa-z
.p
could be empty and contains only lowercase lettersa-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的更多相关文章
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
- 【leetcode】10.Regular Expression Matching
题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- [Leetcode][Python][DP]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode OJ:Regular Expression Matching(正则表达式匹配)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【LeetCode】010. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【LeetCode】10.Regular Expression Matching(dp)
[题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
随机推荐
- warning: React does not recognize the xxx prop on a DOM element
这是React不能识别dom元素上的非标准attribute报出的警告,最终的渲染结果中React会移除这些非标准的attribute. 通常{...this.props}和cloneElement( ...
- POI 10.28
[POI2015]KUR 不考虑构造原串再匹配 考虑开始位置满足什么条件才能匹配. 显然,开始位置确定,后面的字符都确定了. 而且,a,n互质,所以必然能遍历n的剩余系,从不同位置开始,初始的a*s+ ...
- UIViewContentMode的各种效果
UIViewContentMode的各种效果: 首先它是枚举类型的数据,表示为下所示: typedef enum { UIViewContentModeScaleToFill, ...
- bzoj 4206 最大团 几何+lis
最大团 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 142 Solved: 65[Submit][Status][Discuss] Descrip ...
- YAML schema reference
YAML schema reference 10/30/2018 14 minutes to read Azure Pipelines Here's a detailed reference guid ...
- uploadify图片上传配置
参考:http://www.cnblogs.com/XuebinDing/archive/2012/04/26/2470995.html 官网地址:http://www.uploadify.com/ ...
- 图连通性【tarjan点双连通分量、边双联通分量】【无向图】
根据 李煜东大牛:图连通性若干拓展问题探讨 ppt学习. 有割点不一定有割边,有割边不一定有割点. 理解low[u]的定义很重要. 1.无向图求割点.点双联通分量: 如果对一条边(x,y),如果low ...
- hadoop+spark 集群的安装
1.安装连接 https://www.cnblogs.com/zengxiaoliang/p/6478859.html
- TensorFlow 模型保存和导入、加载
在TensorFlow中,保存模型与加载模型所用到的是tf.train.Saver()这个类.我们一般的想法就是,保存模型之后,在另外的文件中重新将模型导入,我可以利用模型中的operation和va ...
- Eureka服务续约(Renew)源码分析
主要对Eureka的Renew(服务续约),从服务提供者发起续约请求开始分析,通过阅读源码和画时序图的方式,展示Eureka服务续约的整个生命周期.服务续约主要是把服务续约的信息更新到自身的Eurek ...