蜗牛慢慢爬 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 ...
随机推荐
- 【BZOJ1018】[SHOI2008]堵塞的交通
[BZOJ1018][SHOI2008]堵塞的交通 题面 bzoj 洛谷 洛谷 题解 菊队讲要用线段树维护连通性,但是好像没人写 解法一 将所有的加边删边离线,然后以最近删除时间为边权,$LCT$维护 ...
- CentOS7 msmtp+mutt发送邮件
一.安装软件 # msmtp软件各版本下载地址:https://marlam.de/msmtp/download/ [root@--- ~]# wget https://marlam.de/msmtp ...
- TMS320VC5509的外部中断
1. 外部中断引脚INT0-INT4,INT2-平时是低电平,INT3-平时是高电平 2. 不过中断不支持设置上升沿和下降沿触发,中断就是中断,我估计应该是平时是高电平,然后低电平触发中断,代码比较简 ...
- sqlyog 可视化查看数据库关系并创建外键
+ 一个架构设计器,把表拖进来即可查看数据库关系 如果要建立外键,需要在两个要被建立的外键之间进行操作(经过验证不需要都为主键),然后从用鼠标把子外键拖到父外键中,就可以关联上了. 参考: https ...
- centos7 安装postgres9.4
1.安装postgres资源:> yum install https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7-x86_ ...
- js.ajax优缺点,工作流程
1.ajax的优点 Ajax的给我们带来的好处大家基本上都深有体会,在这里我只简单的讲几点: 1.最大的一点是页面无刷新,在页面内与服务器通信,给用户的体验非常好. 2.使用异步方式与服务器通信,不 ...
- net 快速打印日志
System.IO.File.AppendAllText(@"F:WriteText.txt", "日志内容“+"\r\n");
- pdo的用处,用法
PDO主要是用来对数据库进行访问的.PDO扩展为PHP访问数据库定义了一个轻量级的一致接口,不同数据库在访问时,采用相同方法名称,解决了连接数据库不统一问题.PDO扩展自身并不能实现任何数据库功能,必 ...
- 使用Fiddler进行Web接口测试
一.Fiddler简介1.为什么是Fiddler?抓包工具有很多,小到最常用的web调试工具firebug,达到通用的强大的抓包工具wireshark.为什么使用fiddler?原因如下: A)Fir ...
- Ubuntu安装Oh My Zsh
1. 安装ZSH sudo apt-get install zsh 安装完后需要注销或重启才能生效.注销或重启后打开终端,会出现ZSH的界面,选择(2) 2. 安装Oh My Zsh sh -c &q ...