leetcode-algorithms-10 Regular Expression Matching

Given an input string (s) and a pattern (p), 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).

Note:

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like . or *.

Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".

Example 3:

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".

Example 5:

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false

解法1

class Solution
{
public:
bool isMatch(string s, string p)
{
if (p.empty()) return s.empty(); //检查首字母是否匹配,因为'*'通配符要有前置元素,所以不需要考虑
bool match = (!s.empty() && (p[0] == s[0] || p[0] == '.')); if (p.size() >= 2 && p[1] == '*')
//第二个字母为'*',这有2种情况,'*'号为0时和'*'号不为0时
return (isMatch(s, p.substr(2)) || (match && isMatch(s.substr(1), p)));
else
//第二个字母不为'*'继续.
return match && isMatch(s.substr(1), p.substr(1)); return false;
}
};

时间复杂度: O(min(n, m)).

空间复杂度: O(1).

解法2

利用C++11 regex正规库

class Solution
{
public:
bool isMatch(string s, string p)
{
return std::regex_match(s, std::regex(p));
}
};

链接: leetcode-algorithms 目录

leetcode-algorithms-10 Regular Expression Matching的更多相关文章

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

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

  2. 【一天一道LeetCode】#10. Regular Expression Matching

    一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...

  3. 【leetcode】10.Regular Expression Matching

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

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

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

  5. 【LeetCode】10.Regular Expression Matching(dp)

    [题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...

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

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

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

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

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

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

  9. 刷题10. Regular Expression Matching

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

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

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

随机推荐

  1. (转) AdversarialNetsPapers

      本文转自:https://github.com/zhangqianhui/AdversarialNetsPapers AdversarialNetsPapers The classical Pap ...

  2. DownAlbum:Chrome的pinterest批量下载插件

    一.DownAlbum安装 二.DownAlbum使用 点击DownAlbum图标. 选择Normal. 会出现一个加载的弹窗,等待片刻会打开一个新的窗口. 按Ctrl+S,即可保存相册所有图片. 图 ...

  3. JAVA 上传文件到linux上并解压缩

    package com.inborn.inshop.controller.mkt; import java.io.*; import ch.ethz.ssh2.ChannelCondition;imp ...

  4. matplotlib python

    #导入包 import matplotlib.pyplot as plt import numpy as np # 从[-1,1]中等距去50个数作为x的取值 x = np.linspace(-1, ...

  5. Sublime Text3 插件:DocBlockr与javascript注释规范

    原:http://www.ithao123.cn/content-719950.html 1.引子 在写代码的时候,尤其是写脚本,最需要注释了.目前脚本.样式的注释格式都有一个已经成文的约定规范(这些 ...

  6. sprinf sprintf_s 的用法

    函数功能: 将数据格式化输出到字符串 函数原型: int sprintf( char *buffer, const char *format [,argument] ... ) 注意这里的buffer ...

  7. 【Python】【容器 | 迭代对象 | 迭代器 | 生成器 | 生成器表达式 | 协程 | 期物 | 任务】

    Python 的 asyncio 类似于 C++ 的 Boost.Asio. 所谓「异步 IO」,就是你发起一个 IO 操作,却不用等它结束,你可以继续做其他事情,当它结束时,你会得到通知. Asyn ...

  8. node.js中的http.response.end方法使用说明

    转载自:http://m.jb51.net/article/58468.htm 本文介绍了http.response.end的方法说明.语法.接收参数.使用实例和实现源码,需要的朋友可以参考下 方法说 ...

  9. 服务器不能设置内容类型HTTP头信息后发送

    昨天一个用户反映,导出excel失败,我测试了一下,数据量小没有问题,数据量稍微大就会出现这个问题.咨询度娘也没有找到合适的解决方法,突然想到系统中,其他模块有下载excel附件的功能,但是从没有出现 ...

  10. SQL关于WHERE 的计算次序

    WHERE可包含任意数目的AND和OR操作符.允许两者结合以进行复杂 和高级的过滤. 但是OR和AND操作符是有先后次序的. 比如,原意是想找出 3班和5班年龄为21岁的同学,使用 :SELECT * ...