Implement regular expression matching with support for '.' and '*'.

  1. '.' Matches any single character.
  2. '*' Matches zero or more of the preceding element.
  3.  
  4. The matching should cover the entire input string (not partial).
  5.  
  6. The function prototype should be:
  7. bool isMatch(const char *s, const char *p)
  8.  
  9. Some examples:
  10. isMatch("aa","a") false
  11. isMatch("aa","aa") true
  12. isMatch("aaa","aa") false
  13. isMatch("aa", "a*") true
  14. isMatch("aa", ".*") true
  15. isMatch("ab", ".*") true
  16. isMatch("aab", "c*a*b") true

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

  1. class Solution {
  2. public:
  3. bool isMatch(string s, string p) {
  4. return dfsIsMatch(s,p,0,0);
  5. }
  6.  
  7. bool dfsIsMatch(const string& s, const string& p, int sIndex, int pIndex){
  8. if (p[pIndex] == '\0') //结束条件:s若是'\0',p必须也是'\0'
  9. return s[sIndex] == '\0';
  10.  
  11. if (p[pIndex+1] == '*') {
  12. /* '.' means any character (except '\0')
  13. * '.' means repeat 0 or more times
  14. * '.*' means repeat '.' 0 or more times
  15. */
  16. while ((s[sIndex] != '\0' && p[pIndex] == '.') || s[sIndex] == p[pIndex]) { //'.'可以与除'\0'以外的任何字符匹配
  17. if (dfsIsMatch(s, p, sIndex, pIndex+2)) //p[pIndex] repeat 0 times
  18. return true;
  19.  
  20. sIndex += 1;//p[pIndex]在原基础上repeat次数+1
  21. }
  22.  
  23. return dfsIsMatch(s, p, sIndex, pIndex+2); //when s[sIndex] != p[pIndex] && p[pIndex] != '.'(此时只有s[sIndex]==p[pIndex]=='\0'时可能return true)
  24. }
  25. else if ((s[sIndex] != '\0' && p[pIndex] == '.') || s[sIndex] == p[pIndex]) {
  26. return dfsIsMatch(s, p, sIndex + 1, pIndex + 1);
  27. }
  28.  
  29. return false;
  30. }
  31. };

法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。

  1. class Solution {
  2. public:
  3. bool isMatch(string s, string p) {
  4. int sLen=s.length();
  5. int pLen = p.length();
  6. bool dp[sLen+1][pLen+1]={false}; //[0]for string NULL
  7.  
  8. //initialize
  9. dp[0][0]=true;
  10. for(int j = 1; j <= pLen; j++){
  11. dp[0][j]=j>=2 && dp[0][j-2] && p[j-1]=='*'; //* each other letter
  12. }
  13.  
  14. //state transfer
  15. for(int i = 1; i <= sLen; i++){
  16. for(int j = 1; j <= pLen; j++){
  17. if(p[j-1]=='*'){
  18. dp[i][j]=(j>=2 && dp[i][j-2])/* repeat 0 times*/
  19. ||(dp[i][j-1])/*repeat 1 time*/
  20. ||(j>=2 && (s[i-1]==p[j-2] || p[j-2]=='.') && dp[i-1][j])/*repeat several times*/;
  21. }
  22. else{ //p[j-1]!='*'
  23. dp[i][j]=dp[i-1][j-1] && (s[i-1]==p[j-1] || p[j-1]=='.');
  24. }
  25. }
  26. }
  27.  
  28. return dp[sLen][pLen];
  29. }
  30. };

  

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. 执行Chrome自动化时--正在受到自动软件的控制的显示屏蔽

    用selenium启动,浏览器出现‘Chrome正在受到自动软件的控制’ 屏蔽的方法: # coding:utf-8 from selenium import webdriver # 加启动配置 op ...

  2. 正则的使用及replace细讲

    1.var reg=/./ 与 var reg=/\./的区别? .代表任意一个字符 \.而后者代表这个字符串中得有一个. 2.?的使用 如果单独的一个字符后面带? /\d?/ 代表1个或0个这个字符 ...

  3. [转载] ./configure,make,make install的作用

    1.configure,这一步一般用来生成 Makefile,为下一步的编译做准备,你可以通过在 configure 后加上参数来对安装进行控制,比如代码:./configure –prefix=/u ...

  4. FDD vs TDD

    双工方式 FDD vs TDD  频分双工(FDD) 收发信各占用一个频率. 优点是收.发信号同时进行,时延小,技术成熟,缺点是设备成本高.  时分双工(TDD) 收发信使用同一个频率,但使用不同 ...

  5. HTML5中常用的标签(及标签的属性和作用)

    1.标签:<!DOCTYPE>作用:声明是文档中的第一成分,位于<html>标签之前. 2.标签:<html>作用:此元素可告知浏览器其自身是一个HTML文档.属性 ...

  6. 目前学习的爬取小数据图片zzz

    import os import threading import re import time from lxml import etree all_img_urls = [] # 图片列表页面的数 ...

  7. pycharm最新版新建工程没导入本地包问题:module 'selenium.webdriver' has no attribute 'Firefox'

    前言 最新版的pycharm做了很大的改变,新建工程的时候,默认不导入本地的安装包,这就导致很多小伙伴踩坑了... 明明已经pip安装过selenium了,但是却报AttributeError:mod ...

  8. ByteCache

    private static class ByteCache { private ByteCache(){} //256个元素,用于缓存-128到127 static final Byte cache ...

  9. php获取服务器信息类

      <?php/**+------------------------------------------------------------------------------* 获取服务器信 ...

  10. oracle跟踪sql语句

    oracle跟踪sql语句 select * from v$sql 查询客户端电脑名称的ID select terminal, SID,SERIAL#  from v$session where  ( ...