Question

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

Answer

  题意很容易明白,根据pattern判断string是否有效,就是说string是否符合pattern模式。

  string中的每个字符从a-z中选择,string可以为空。

  pattern由任意数量的a-z字母、’ . ' 以及 a*、b*到 z* 、. * 组成的字符串。

  (字符+星号*)表示有任意(包括零个)个字符,如a*表示有任意多个a,.* 表示有任意个字符。

  比如

Input:
s = "aa" p = "a"

我们根据p逐字匹配,s第一个字符与p第一个字符匹配,然而p没有第二个字符了,s还有一个a,就是s不与p匹配,所以输出false;

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

  和第一个例子一样,p的前两个字符和s匹配,p第三个字符加*表示有任意数量的s,这里匹配到s的第四个字符,接着s第五到第七个字符(iss)也与p相匹配,然后s的第八个字符i并没有在p中有所匹配,所以也输出false;

  根据上面的例子,我们可以开始考虑解决我们的问题了。

  首先,如果不考虑p中(字符+*)的组合,问题会特别简单,我们只需将p与s中的字符一一匹配即可。

  然而,如果有(字符+*)的存在,问题就有些复杂了。比如

Input:
s = "aab"

  你会发现

p = "a*ab" 或 p = "a*b" 或 p = "a*.b"

  都与s匹配。换句话说你不知道a*能匹配到多少个a,你不知道什么时候a*才算结束,比如说p = "a*ab"中a*对应s一个a,p="a*b"对应s两个a,p="a*.b"对应s一个a。最初我以为自己能根据a*后的字符判断a*与多少个a匹配,最后觉得思维有点乱便放弃了,主要原因是a*与多少个a匹配都有可能,比如p='a*b',若a*只与一个a匹配,后面就可能匹配不上了。

  所以,我们不妨将a*所有情况都考虑。逐个字符匹配时,若遇到类似a*这类型的两个字符时,我们分别考虑以下两种情况:

  ① a*与0个a匹配。

  ② a*与1个a匹配,然后p还是以a*与s的下一个字符匹配(就是说下次匹配继续考虑①②这两种情况)。

  这样就能考虑到a*的所有情况了。

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".

  然后根据以上算法我们编写代码:

    bool isMatch(string s, string p) {
int s_len = s.length();
int p_len = p.length();
if (p_len == )
return s_len == ;
bool match = (s_len != ) && (s[] == p[] || p[] == '.');
if (p_len >= && p[] == '*')
{
return isMatch(s, p.substr()) || (match && isMatch(s.substr(), p));
}
else if (p_len >= )
{
return match && isMatch(s.substr(), p.substr());
}
}

  代码思路时很清晰的,match判断下一个字符是否匹配,if (p_len >= 2 && p[1] == '*') 对 是否出现类似a*的情况 进行判断,若出现就像上面说的两种情况进行操作,若没有出现则一一匹配。

  最后要注意一下边界,特别是s为空,p为非空这两种情况。

  p为空,s为非空已经能说不匹配了; p,s都为空,说明匹配成功。

  p为非空,s为非空说明还能继续匹配,因为每次调用函数都是缩小字符串长度的。

  s为空,p为非空,有可能是p中出现类似a*的情况,也有可能p还剩a-z或'.'字符,也有可能是这两种组合。

  如果是p中形如a*正在匹配,我们从p中去掉它,因为相当于没有a。

  如果是后一种情况正在匹配,则匹配失败。

												

Leetcode Week1 Regular Expression Matching的更多相关文章

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

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

  2. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

  3. [LeetCode][Python]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...

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

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

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

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

  6. [LeetCode] 10. Regular Expression Matching

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

  7. 【leetcode】Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  8. 【leetcode】Regular Expression Matching (hard) ★

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

  9. 【JAVA、C++】LeetCode 010 Regular Expression Matching

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

随机推荐

  1. Starting MySQL... ERROR! The server quit without updating PID file (/home/mysql-5.6.43/data/localhost.localdomain.pid).

    启动MySQL出现如下错误 May :: localhost mysqld: Starting MySQL... ERROR! The server quit without updating PID ...

  2. bootstrap-daterangepicker

    1,依赖关系 使用之前需要引用bootstrap.css   daterangpicker.css    jquery.js   boostrap.js  moment.js   daterangpi ...

  3. 题解【[USACO05NOV]奶牛玩杂技】

    \[ \texttt{Description} \] 有 \(n\) 头牛,每头牛都有自己的体重 \(W_i\) 和力量 \(S_i\) . 将这 \(n\) 头牛摞在一起,每头牛的压扁指数定义为:压 ...

  4. springboot 后台框架平台 mybatis 集成代码生成器 shiro 权限 websocket

    1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器)freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块2. ...

  5. 01.JS语法规范、变量与常量

    前言:    学习一门编程语言的基本步骤   (01)了解背景知识  (02)搭建开发环境  (03)语法规范  (04)常量和变量 2.JS的开发环境 (1)浏览器自带的JS解释器(js引擎) (2 ...

  6. (三)LoadRunner术语认识

    场景:主要表现为controller中设计与执行测试用例中的用户场景.主要工作有,在controller中选择虚拟用户脚本.设置虚拟用户数量.配置虚拟用户运行时的行为.选择负载发生器.设置执行时间等. ...

  7. STT-MRAM存在的两个弊端

    随着自旋转移矩效应的发现以及材料和结构的优化,基于自旋转移矩效应的STT-MRAM器件应运而生.自从自旋转移矩效应被证实以来,一方面研究人员通过大量的努力尝试降低磁化反转的临界电流,增加热稳定性:另一 ...

  8. Java 代码块详解

    注:本文出自博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 注:本文原链接:https://www.cnblogs.com/chloneda/p/java-c ...

  9. 牛客网剑指offer第13题——调整数组顺序使得奇数位于偶数前面

    题目来源:剑指offer 题目: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变 ...

  10. Python安装和配置环境变量(简明教程)

    声明:借鉴Python 简明教程 安装我们在本书中提到的「Python 3」指的是 Python 版本大于或等于 Python 3.6.0. 针对Python3.6.版本:注意数据的缓存机制 # ## ...