蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]
题目
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).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
翻译
实现正则表达式中的 .
和 *
匹配
Hints
Related Topics: String, Dynamic Programming, Backtracking
第一个方法就是比较暴力的BF:
- 比较字符串 s 和 p 的 从 i 和 j 开始的子串是否匹配,用递归的方法直到串的最后,最后回溯得到结果
- 那么假设现在走到了 s 的 i 位置和 p 的 j 位置:
- p[j+1]不是 '*',那么判断 s[i] 和 p[j] 是否相同(注意 '.')不同返回 false,相同继续递归下一层 i+1,j+1
- p[j+1]是 '*',那么看 s[i] 开始的子串,如果 s[i],s[i+1] ... s[i+k] 都等于 p[j],那么它们都可能是合适的匹配,这样递归就要尝试剩下的 (i,j+2),(i+1,j+2),(i+k,j+2)
另一个方法就是动态规划:
- 通过 dp[len(s)+1][len(p)+1]来存储计算过的结果
- 遍历的话可以以 p 为外循环,也可以以 s 为外循环
if p[j] == s[j]: dp[i][j]=dp[i-1][j-1]
if p[j] == '.': dp[i][j]=dp[i-1][j-1]
if p[j] == '*':
if p[j-1] != s[i]: dp[i][j]=dp[i][j-2] //a*匹配空
if p[j-1] == s[i] or p[j-1] == '.':
dp[i][j] = dp[i][j-1] or //a*匹配一个a
dp[i][j] = dp[i][j-2] or //a*匹配空
dp[i][j] = dp[i-1][j] //a*匹配两个a
代码
Java
class Solution {
public boolean isMatch(String s, String p) {
if(s.length()==0 && p.length()==0){
return true;
}
if(p.length()==0){
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)=='*' && i>0 && dp[0][i-1])
dp[0][i+1] = true;
}
for(int i=0;i<s.length();i++){
for(int j=0;j<p.length();j++){
if(p.charAt(j)=='.')
dp[i+1][j+1] = dp[i][j];
if(p.charAt(j)==s.charAt(i))
dp[i+1][j+1] = dp[i][j];
if(p.charAt(j)=='*'){
if(p.charAt(j-1)!=s.charAt(i) && p.charAt(j-1)!='.'){
dp[i+1][j+1] = dp[i+1][j-1];
}else{
dp[i+1][j+1] = (dp[i+1][j]||dp[i+1][j-1]||dp[i][j+1]);
}
}
}
}
return dp[s.length()][p.length()];
}
}
Python
class Solution(object):
def helper(self, s, p, i, j):
if j == len(p):
return i==len(s)
if j == len(p)-1 or p[j+1]!='*':
if i==len(s) or (s[i]!=p[j] and p[j]!='.'):
return False
else:
return self.helper(s,p,i+1,j+1)
while i<len(s) and (p[j]=='.' or s[i]==p[j]):
if self.helper(s,p,i,j+2):
return True
i += 1
return self.helper(s,p,i,j+2)
def isMatch(self, s, p):
return self.helper(s,p,0,0)
蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- [leetcode]10. Regular Expression Matching正则表达式的匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode] 10. Regular Expression Matching ☆☆☆☆☆
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- node auto run / node 自动运行
http://stackoverflow.com/questions/20445599/auto-start-node-js-server-on-boot http://stackoverflow.c ...
- Python urllib模块学习
urlopen()与urlretrieve() 1.urlopen()方法 urllib.urlopen(url[, data[, proxies]]) :创建一个表示远程url的类文件对象,然后像本 ...
- Java线程和多线程(十四)——Synchronized关键字解析
曾经有一个比较有趣的面试问题,那就是,关于使用synchronized关键字,是用在方法上面尾号,还是用在一个代码块上面为好? 答案就是使用锁定代码块为更好.因为这样不会锁定对象.当synchroni ...
- JavaWeb总结(二)
Web服务器的缺陷 Web服务器是被设计用来向客户端提供HTTP服务的,它只能向客户端提供静态网页内容.静态页面是原封不动的待在Web服务器目录中,服务器找到静态网页,并把它原样传回到客户端.每个客户 ...
- RTTI(运行时类型识别)
C++为了能够在运行时正确判断一个对象确切的类型,加入了RTTI和type_info. type_info 为每一个类型增加一个type_info对象. 为了能够在运行时获得对象的类型信息type_i ...
- 15 [网络编程]-ssh远程命令
1.执行命令os.system('ls') os.system 返回1 or 0 ,不能当做数据发送 # windows # dir 查看某个文件夹下子自文件名与子文件夹名 # ipconfig 查 ...
- 41-mysql作业
1 2 3 4
- 【HNOI2015】菜肴制作
题面 题解 这道题目首先可以想到拓扑排序,但是肯定不是字典序最小的排列. 比如说,有\(4\)种菜,限制为\(2 \to 4, 3 \to 1\),那么如果求字典序最小的排列会算出\((2, 3, 1 ...
- java日志框架log4j详细配置及与slf4j使用教程
一.log4j基本用法 首先,配置log4j的jar,maven工程配置以下依赖,非maven工程从maven仓库下载jar添加到“build path” 1 2 3 4 5 <dependen ...
- ffmpeg——关于视频压缩
这篇博客主要讲有关于视频压缩的问题,解决视频文件太大,不便于下载,占用存储空间过大等问题,在缩小视频大小的同时,保证视频的观看质量.主要讲以下几点: 1.压缩视频工具ffmpeg 2.压缩视频的技术参 ...