题目


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 . or * .

Example1:

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

  Output:false

  Explanation:"a" does not match the entire string "a"

Example2:

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

  Output:true

  Explanation:".*" means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it become "a".

Example3:

  Input:s = "ab" p=".*"

  Output:true

  Explanation:"." means " zero or more () of any character (.) " .

思路


动态规划

Step1. 刻画一个最优解的结构特征

\(dp[i][j]\)表示\(s[0,\cdots,i-1]\)与\(p[0,\cdots,j-1]\)是否匹配

Step2. 递归定义最优解的值

1.\(p[j-1] == s [i-1]\),则状态保存,\(dp[i][j] = dp[i-1][j-1]\)

2.\(p[j-1] ==\) ..与任意单个字符匹配,于是状态保存,\(dp[i][j] = dp[i-1][j-1]\)

3.$p[j-1] == $**只能以X*的形式才能匹配,但是由于*究竟作为几个字符匹配不确定,此时有两种情况:

  • \(p[j-2] != s[i-1]\),此时\(s[0,\cdots,i-1]\)与\(p[0,\cdots,j-3]\)匹配,即\(dp[i][j] = dp[i][j-2]\)
  • \(p[j-2] == s[i-1]\) 或者 $p[j-2] == $ .,此时应分为三种情况:

    *作为零个字符,\(dp[i][j] = dp[i][j-2]\)

    *作为一个字符,\(dp[i][j] = dp[i][j-1]\)

    *作为多个字符,\(dp[i][j] = dp[i-1][j]\)
Step3. 计算最优解的值

根据状态转移表,以及递推公式,计算dp[i][j]

Tips


数组初始化(python)

(1)相同的值初始化(一维数组)
#方法一:list1 = [a a a ]
list1 = [ a for i in range(3)]
#方法二:
list1 = [a] * 3
(2)二维数组初始化

初始化一个\(4*3\)每项固定为0的数组

list2 = [ [0 for i in range(3)] for j in range(4)]

C++

class Solution {
public:
bool isMatch(string s, string p) {
int m = s.length(),n = p.length();
bool dp[m+1][n+1];
dp[0][0] = true;
//初始化第0行,除了[0][0]全为false,因为空串p只能匹配空串,其他都无能匹配
for (int i = 1; i <= m; i++)
dp[i][0] = false;
//初始化第0列,只有X*能匹配空串
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][j - 2] || (s[i - 1] == p[j - 2] || p[j - 2] == '.') && dp[i - 1][j];
}
else //只有当前字符完全匹配,才能传递dp[i-1][j-1] 值
{
dp[i][j] = (p[j - 1] == '.' || s[i - 1] == p[j - 1]) && dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}
};

Python

def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
len_s = len(s)
len_p = len(p)
dp = [[False for i in range(len_p+1)]for j in range(len_s+1)]
dp[0][0] = True
for i in range(1, len_p + 1):
dp [0][i] = i>1 and dp[0][i - 2] and p[i-1] == '*'
for i in range (1, len_s + 1 ):
for j in range(1, len_p + 1):
if p[j - 1] == '*':
#状态保留
dp[i][j] = dp[i][j -2] or (s[i-1] == p[j-2] or p[j-2] == '.') and dp[i-1][j]
else:
dp[i][j] = (p[j-1] == '.' or s[i-1] == p[j-1]) and dp[i-1][j-1]
return dp[len_s][len_p]

10. Regular Expression Matching[H]正则表达式匹配的更多相关文章

  1. LeetCode OJ:Regular Expression Matching(正则表达式匹配)

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

  2. Regular Expression Matching,regex,正则表达式匹配,利用动态规划

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

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

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

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

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

  5. 刷题10. Regular Expression Matching

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

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

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

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

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

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

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

  9. 10. Regular Expression Matching正则表达式匹配

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

随机推荐

  1. [Offer收割]编程练习赛37

    热门号码 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> ...

  2. MongoDB 博客截图之二

    使用内置帮助help() 基本命令示例 来源:MongoDB基本管理命令 - 千与的专栏 - 博客频道 - CSDN.NET

  3. Nagios Windows客户端NSClient++ 0.4.x安装配置

    NSClient++ 0.3.x和NSClient++ 0.4.x的配置完全不一样,官方的文档也没有全部更新.我记录下自己的一些操作.   一.下载安装NSClient++ 1.到http://nsc ...

  4. 使用Eric构建Caffe应用程序-Baby年龄识别

    训练好的Caffe网络结构,可以固定下来,直接载入程序作为数据库接口使用.本文使用Eric构建运行于Python环境下的图片识别应用程序,因为Eric使用QT作为GUI,且有Python的接口,可直接 ...

  5. 【技术累积】【点】【java】【9】Optional

    基础概念 java8引入的,java9有加强 Google公司出品 旨在更好的处理NullPointException 创建Optional实例和基础使用 Optional op1 = Optiona ...

  6. [Shell] echo/输出 中引用命令

    # 这样是错误的,是引用变量 echo "/Users/${whoami}/Desktop" >>> /Users//Desktop # 正确的写法应该是使用`` ...

  7. 优动漫PAINT是什么?有哪些功能和特色

    优动漫PAINT软件介绍: 优动漫PAINT是一款功能强大的漫画制作软件,该软件可以帮助漫画创作人员制作出完美,惟妙惟肖的漫画作品,它搭载了绘制漫画和插画所需的所有功能--丰富的笔工具.超强的笔压感应 ...

  8. Centos7 执行firewall-cmd –permanent –add-service=mysql报错“ModuleNotFoundError: No module named 'gi'”

    因为目前环境Python3.x与Python2.x版本并存,所以导致以上问题. 解决方法: 第一步,vim  /usr/bin/firewall-cmd, 将#!/usr/bin/python -Es ...

  9. JXLS生成excel并自定义单元格样式

    本人最近需要每天统计数据表,并每周一发送统计结果的邮件,所以写了个springboot老自动完成工作.项目地址为:https://github.com/707293891/springboot 其中某 ...

  10. 使用ant design pro搭建项目

    脚手架搭建 git clone --depth=1 https://github.com/ant-design/ant-design-pro.git my-project 然后 cd my-proje ...