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

回溯算法 Backtracking

乍看之下,该问题很像一个 字符串匹配 问题。那么我们怎么样去匹配 ' * ' ?
通用的解决算法是,使用 贪心算法: 【尽可能多的匹配 ' * ' 之前的字符】。下面我们分析这种算法可能存在的问题。

s = “abbbc”, p = “ab*c”
假设我们先匹配了 字符 'a',然后我们开始匹配 ‘b*’,我们忽略所有的s中的 'b',到最后,我们遇到字符 'c' ,p和s匹配。

s = “ac”, p = “ab*c”
在匹配‘a’后,因为s中没有任何字符'b',所以略过 'b*',直接匹配最后一个字符 'c',p和s匹配。
到目前为止,贪心算法 表现良好。接着看:

s = “abbc”, p = “ab*bbc”
当匹配‘b*’,我们会略过s中的所有字符'b';之后所有的'b'就无法再被'bb'匹配,使用 贪心算法,这个匹配的返回值将会是false,但我们预计的是 true。
有些人可能会对此提出改进,在s中,计算连续的字符 'b' 的个数,如果个数比在p中‘b*’后的连续b个数小或相等,那么我们认为匹配。

看起来似乎是解决了该问题。我们再看下一个例子:
s = “abcbcd”, p = “a.*c.*d”
这里,p中的“.*” 意味着‘.’ 重复0 or 随意次。因为 ‘.’ 可以匹配任意字符,所以无法确定 ‘.’ 到底应该重复多少次。p中的 ‘c’ 到底去匹配第一个,还是第二个'c' ,不得而知。

所以我们需要使用 回溯backtracking 当匹配失败的时候。 我们返回到上一次匹配成功的状态,并用‘*‘ 匹配更多的s中的字符。 该算法需要使用到 递归recursion。

递归recursion 在以下两种情况下停止:                                                         
  • 如果p中的下一个字符不是 ‘*’, 则它必须匹配当前s中的字符。并继续用下一个字符,去匹配s中的字符。
  • 如果p中的下一个字符是 ‘*’, 那我们必须使用 暴力搜索brute force 去匹配s中的当前字符,不断重复,直到不再匹配

代码

120ms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
    bool isMatch(string s, string p) {
        if (p.size() == 0) return s.size() == 0;//一定不能写反
        if (p[1] != '*')
            return (s[0] == p[0] || p[0] == '.'  && !s.empty()) && isMatch(s.substr(1), p.substr(1));
        int i = 0;
        while (s[i] == p[0] || (p[0] == '.' && i < s.size())) {
            if (isMatch(s.substr(i), p.substr(2))) return true;
            ++i;
        }
        return isMatch(s.substr(i), p.substr(2));
    }
};

现实应用中,实际上是使用 正则表达式 grep tool 工具来匹配字符的。

动态规划 Dynamic Programming

首先说明一下规则,dp[m][n] 数组返回值boolean.。它告诉我们长度n的正则表达式,是否匹配长度m的字符串。

  1. dp[m][n]: if s[0..m-1] matches p[0..n-1]
下面看算法的base case:
dp[0][0] 永远是 true
dp[m][0] 当 m > 0, 永远false。空的正则表达式不会匹配任何字符串。
dp[0][n] 可以是 true 或 false。如果p[j - 1] 是'*' 并且p[0..j - 3] 匹配空字符串,那么p[0.., j - 3, j - 2, j - 1] 也匹配空字符串s,
1
2
3
4
5
6
dp[0][0] = true;
for (int i = 1; i <= m; i++)
    dp[i][0] = false;
// p[0.., j - 3, j - 2, j - 1] matches empty if p[j - 1] is '*' and p[0..j - 3] matches empty
for (int j = 1; j <= n; j++)
    dp[0][j] = j > 1 && '*' == p[j - 1] && dp[0][j - 2];
下面来关注 递推关系 recurrence relationship
  1. * dp[i][j]: if s[0..i-1] matches p[0..j-1]
  2. * if p[j - 1] != '*'
  3. * dp[i][j] = dp[i - 1][j - 1] && s[i - 1] == p[j - 1]
  4. * if p[j - 1] == '*', denote p[j - 2] with x
  5. * dp[i][j] is true if any of the following is true
  6. * 1) "x*" repeats 0 time and matches empty: dp[i][j - 2]
  7. * 2) "x*" repeats >= 1 times and matches "x*x": s[i - 1] == x && dp[i - 1][j]
  8. * '.' matches any single character

从而可以得到下面的代码:12ms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
    bool isMatch(string s, string p) {
        int m = s.size(), n = p.size();
        vector<vector<bool> > dp(m + 1, vector<bool>(n + 1, false));
 
        dp[0][0] = true;
        for (int i = 1; i <= m; ++i) dp[i][0] = false;
        for (int j = 1; j <= n; ++j) dp[0][j] = j > 1 && p[j - 1] == '*' && dp[0][j - 2];
 
        for (int i = 1; i <= m; ++i)
            for (int j = 1; j <= n; ++j) {
                if (p[j - 1] != '*')
                    dp[i][j] = dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j-1] == '.');
                else
                    dp[i][j] = dp[i][j - 2] //如果* 代表不重复,即空字符
                    || dp[i - 1][j] && (s[i - 1] == p[j - 2] || '.' == p[j - 2]);//* 代表重复>=1次
            }
        return dp[m][n];
    }
};


10. Regular Expression Matching的更多相关文章

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

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

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

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

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

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

  4. 刷题10. Regular Expression Matching

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

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

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

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

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

  7. [LeetCode] 10. Regular Expression Matching

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

  8. 【leetcode】10.Regular Expression Matching

    题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

  9. Java [leetcode 10] Regular Expression Matching

    问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

随机推荐

  1. js 判断是不是手机访问

    方法一: if(window.navigator.userAgent.toLowerCase().indexOf('mobile')==-1) { alert("pc端") } e ...

  2. WinForm 无边框窗体和timer控件

    一.无边框窗体 1.控制按钮如何制作就是放置可以点击的控件,不局限于使用按钮或是什么别的,只要可以点击能触发点击事件就可以了 做的好看一点,就是鼠标移入,移出,按下三个事件会让按钮改变样式 如何获取图 ...

  3. Codeforces Round #346 (Div. 2) B Qualifying Contest

    B. Qualifying Contest 题目链接http://codeforces.com/contest/659/problem/B Description Very soon Berland ...

  4. python 基础学习4-with语句

    why use With? 有些事情需要事先进行设置,事后进行处理,with语句提供了一个很好的处理方式,例如文件读写处理,有时候可能忘记关闭文件,with可以很好地处理这种现象. with语句用来简 ...

  5. gridview XML

    GridView动态添加模板列   http://blog.csdn.net/wljhk2006/article/details/24723219 XML与DataTable互转类 http://bl ...

  6. C1FlexGrid小结(转自http://www.cnblogs.com/C1SupportTeam/archive/2012/12/11/2812316.html)

    C1FlexGrid控件来对一个表格格式中的数据进行显示,编辑,组和总结.该表格可以绑定到一个数据源,它可以对自己的数据进行管理. C1FlexGrid控件有一个包含以下元素的丰富的对象模型: 以下的 ...

  7. java变量初始化

    java全局变量会自动初始化,但局部变量不会自动初始化.当我们新建一个对象的时候,java会申请一个区域存放类的数据,而成员变量就是类的数据,也是放在这个内存区域中,jvm申请内存时初始化.而方法中变 ...

  8. java.lang.OutOfMemoryError异常解决方法

    原因:常见的有以下几种:1.内存中加载的数据量过于庞大,如一次从数据库取出过多数据:2.集合类中有对对象的引用,使用完后未清空,使得JVM不能回收:3.代码中存在死循环或循环产生过多重复的对象实体:4 ...

  9. IIS7或IIS7.5 ISAPI和CGI限制找不到的问题记录

    弄了一下午,才找到,选择最上面的节点,如下图,记录备用

  10. 第一百零七节,JavaScript基本包装类型,数据类型的方法

    JavaScript基本包装类型,数据类型的方法 学习要点: 1.基本包装类型概述 2.Boolean类型 3.Number类型 4.String类型 为了便于操作基本类型值,ECMAScript提供 ...