leetcode-algorithms-10 Regular Expression Matching
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-10 Regular Expression Matching的更多相关文章
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
- 【leetcode】10.Regular Expression Matching
题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】10.Regular Expression Matching(dp)
[题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...
- 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 ...
- 刷题10. Regular Expression Matching
一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
随机推荐
- cas4.2.4 登添加验证码
看了很多添加验证码的博文,唯独没有4.24的 重点看第3条,其余的和别人博文大致相同 1.首先在cas工程的web.xml增加验证码功能的支持 <!-- 验证码功能 --> &l ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- HDU 4403 A very hard Aoshu problem(dfs爆搜)
http://acm.hdu.edu.cn/showproblem.php?pid=4403 题意: 给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子. 思路: 数据量比 ...
- BZOJ 1497: [NOI2006]最大获利(最大权闭合图)
http://www.lydsy.com/JudgeOnline/problem.php?id=1497 题意: 思路: 论文题,只要看过论文的话就是小菜一碟啦~ 每个用户群i作为一个结点分别向相应的 ...
- _attribute_character
职业属性控制表 comment 备注 classI ...
- Codeforces 786 B. Legacy
题目链接:http://codeforces.com/contest/786/problem/B 典型线段树优化连边,线段树上的每一个点表示这个区间的所有点,然后边数就被优化为了至多${nlogn}$ ...
- poj 1744 tree 树分治
Tree Time Limit: 1000MS Memory Limit: 30000K Description Give a tree with n vertices,each ed ...
- (一)PHP简介
什么是 PHP? PHP 是 "PHP Hypertext Preprocessor" 的首字母缩略词 PHP 是一种被广泛使用的开源脚本语言 PHP 脚本在服务器上执行 PHP ...
- nginx启动报错:Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' fo
一.背景 这个错误在重启nginx或者启动nginx的时候,经常会出现.我之前也一直认为出现这个错误是因为有程序占用了nginx的进程.但是知其然不知其所以然.每次报错都有点懵逼,所以这边一步步排查错 ...
- SHA-256 加密原理
网络中传输敏感信息的时候通常会对字符串做加密解密处理 SHA-256 加密原理