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

思路:如果下个字符是'*',那么可以重复当前字符0,1,2...次=>带回溯的递归。O(n)=kn,k是*出现的次数。

class Solution {
public:
bool isMatch(string s, string p) {
return dfsIsMatch(s,p,0,0);
} bool dfsIsMatch(const string& s, const string& p, int sIndex, int pIndex){
if (p[pIndex] == '\0') //结束条件:s若是'\0',p必须也是'\0'
return s[sIndex] == '\0'; if (p[pIndex+1] == '*') {
/* '.' means any character (except '\0')
* '.' means repeat 0 or more times
* '.*' means repeat '.' 0 or more times
*/
while ((s[sIndex] != '\0' && p[pIndex] == '.') || s[sIndex] == p[pIndex]) { //'.'可以与除'\0'以外的任何字符匹配
if (dfsIsMatch(s, p, sIndex, pIndex+2)) //p[pIndex] repeat 0 times
return true; sIndex += 1;//p[pIndex]在原基础上repeat次数+1
} return dfsIsMatch(s, p, sIndex, pIndex+2); //when s[sIndex] != p[pIndex] && p[pIndex] != '.'(此时只有s[sIndex]==p[pIndex]=='\0'时可能return true)
}
else if ((s[sIndex] != '\0' && p[pIndex] == '.') || s[sIndex] == p[pIndex]) {
return dfsIsMatch(s, p, sIndex + 1, pIndex + 1);
} return false;
}
};

法II:动态规划。设二位数组dp[i][j]来表示两个string的状态关系,dp[i][j]=true,表示s[0..i-1]匹配p[0..j-1]的结果。O(n)=n2

dp的下标0表示NULL,设置的原因是,比方如果p的第二个字符是*,那么p可以从第三个字符开始与s的第一个字符匹配,也就是dp[0][2]=true。

class Solution {
public:
bool isMatch(string s, string p) {
int sLen=s.length();
int pLen = p.length();
bool dp[sLen+1][pLen+1]={false}; //[0]for string NULL //initialize
dp[0][0]=true;
for(int j = 1; j <= pLen; j++){
dp[0][j]=j>=2 && dp[0][j-2] && p[j-1]=='*'; //* each other letter
} //state transfer
for(int i = 1; i <= sLen; i++){
for(int j = 1; j <= pLen; j++){
if(p[j-1]=='*'){
dp[i][j]=(j>=2 && dp[i][j-2])/* repeat 0 times*/
||(dp[i][j-1])/*repeat 1 time*/
||(j>=2 && (s[i-1]==p[j-2] || p[j-2]=='.') && dp[i-1][j])/*repeat several times*/;
}
else{ //p[j-1]!='*'
dp[i][j]=dp[i-1][j-1] && (s[i-1]==p[j-1] || p[j-1]=='.');
}
}
} return dp[sLen][pLen];
}
};

  

10.Regular Expression Matching (String; Back-Track,DP)的更多相关文章

  1. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

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

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

  3. 刷题10. Regular Expression Matching

    一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...

  4. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

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

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

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

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

  7. 10. Regular Expression Matching

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

  8. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  9. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

随机推荐

  1. 转载-JavaWeb学习总结

    JavaWeb学习总结(五十三)——Web应用中使用JavaMail发送邮件 孤傲苍狼 2015-01-12 23:51 阅读:13407 评论:20     JavaWeb学习总结(五十二)——使用 ...

  2. flume系统使用以及与storm的初步整合

      Flume NG的简单使用可以参考介绍文档:http://blog.csdn.net/pelick/article/details/18193527,图片也来源此blog:       下载完fl ...

  3. nodejs基础: 如何升级Noejs版本

    Node.js的开发非常活跃,它的最新稳定版本也频繁变化,你不时会发现,一个模块不能在你当前的Node版本上使用,此时你需要升级Node 幸运的是,可以用一种非常简单的方法来管理你的Node版本,即使 ...

  4. storm的代码实现

    先模拟产生一些数据 我把这些数据摘一部分下来 2017-06-10 18:25:56,092 [main] [org.apache.kafka.common.utils.AppInfoParser] ...

  5. Xshell批量导入IP地址

    我的xshell被覆盖了~~~结果原来的host没了,很郁闷要一个一个添加,网上找了很长时间在Xshell中批量添加IP的文章,结果都不行. 最后找到了https://blog.netsarang.c ...

  6. nodejs——发送邮件(带附件)

    用到的包是 nodemailer,简单,有效. 1.auth 中的 pass,是指“邮箱第三方登录授权码”,如何获取授权码,以QQ邮箱为例,请点击:http://jingyan.baidu.com/a ...

  7. nodejs——压缩文件_archiver

    工作需要,由于html无法访问并下载带有中文的路径,例子:“127.0.0.1::8088/files/第一张图片.jpg”,所以想到了先将原图片压缩并命名为不带中文的文件名,下载后用户自行解压缩的方 ...

  8. 文件的编辑命令-echo/cat

    touch test.yaml echo "line1 line2" >> test.yaml cat test.yaml line1 line2 # 创建test.y ...

  9. python入门-类(一)

    1 最简单的一个类 class Dog(): """一次模拟小狗的简单尝试""" def __init__(self,name,age): ...

  10. PHP把excel导入mysql数据库最常用的方法

    Posted on 2011-03-25 09:16 PHP博客 阅读(1316) 评论(0)  编辑 收藏 引用 网摘 PHP把excel(xls)文件导入mysql数据库最常用的方法就是先把xls ...