【LeetCode】解题10:Regular Expression Matching(动态规划解法)

leetcode10_Regular Expression Matching[附动态规划] 一.问题描述 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 ent 阅读详情

LeetCode解题 10:Regular Expression Matching(动态规划解法)

Problem 10: Regular Expression Matching [Hard]

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 preceding 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

来源:LeetCode

解题思路

使用动态规划思想。维护数组match[n][m],match[i][j]代表字符串s的前i个和模式p的前j个是否匹配。二重循环遍历sp,match[i][j]根据p[j]和s[i]的关系可以由match[i-1][j-1]等前项转换而来,具体转换公式为:

  1. p [ j ] = s [ i ] p[j] = s[i] p[j]=s[i] 或者 p [ j ] = p[j] = p[j]= '.'时,有
    m a t c h [ i ] [ j ] = m a t c h [ i − 1 ] [ j − 1 ] match[i][j] = match[i-1][j-1] match[i][j]=match[i1][j1]

  2. p [ j ] = p[j] = p[j]= '*'时,情况比较复杂。需要考虑*的前一个字符,即 p [ j − 1 ] p[j-1] p[j1]

    2.1 若 p [ j − 1 ] ≠ s [ i ] p[j-1] \neq s[i] p[j1]=s[i] p [ j − 1 ] ≠ p[j-1] \neq p[j1]= '.',则*只能匹配零个,相当于去掉 p [ j − 1 ] p[j-1] p[j1] p [ j ] p[j] p[j],转换公式为:
    m a t c h [ i ] [ j ] = m a t c h [ i ] [ j − 2 ] match[i][j] = match[i][j-2] match[i][j]=match[i][j2]
    2.2 若 p [ j − 1 ] = s [ i ] p[j-1] = s[i] p[j1]=s[i] 或者 p [ j − 1 ] = p[j-1] = p[j1]= '.',那么*有可能匹配零个或多个。假设当前s###ap###a*,则match[i][j]有可能从不同的前项转移而来:

    i. *匹配多个a的情况,例如:czha|aczha|*。此时s[i]上的a只是a*的延续,转移公式为:
    m a t c h [ i ] [ j ] = m a t c h [ i − 1 ] [ j ] match[i][j] = match[i-1][j] match[i][j]=match[i1][j]
    注意:除了上述例子中*匹配了两个a的情况,转移公式同样适用于匹配一个a的情况。(s = czh|a,p = czha|*,match[i-1][j]代表czhczha*匹配,仍然可以完全匹配。)

    ii. *匹配零个a的情况,例如:czh|aczhaa|*。此时a*是多余的,可以去掉,转移公式为:
    m a t c h [ i ] [ j ] = m a t c h [ i ] [ j − 2 ] match[i][j] = match[i][j-2] match[i][j]=match[i][j2]
    情况i和情况ii中,只要任意一个能匹配上,则 m a t c h [ i ] [ j ] = t r u e match[i][j] = true match[i][j]=true

  3. p [ j ] p[j] p[j]不属于上述任何情况,即 p [ j ] p[j] p[j]不为特殊符号且 p [ j ] ≠ s [ i ] p[j] \neq s[i] p[j]=s[i]时, m a t c h [ i ] [ j ] = f a l s e match[i][j] = false match[i][j]=false

思路参考LeetCode题解

时间复杂度为 O ( n m ) O(nm) O(nm),空间复杂度为 O ( n m ) O(nm) O(nm)

运行结果:
在这里插入图片描述

要点:动态规划

Solution (Java)

class Solution {
    public boolean isMatch(String s, String p) {
        int n = s.length() + 1;
        int m = p.length() + 1;
        if(n == 1 && m == 1) return true;
        if(n > 1 && m == 1) return false;
        boolean[][] match = new boolean[n][m];
        // initialize
        match[0][0] = true;
        match[0][1] = false;
        for(int mj = 2; mj < m; mj++){
            int j = mj - 1;
            if(p.charAt(j) == '*') match[0][mj] = match[0][mj-2];
            else match[0][mj] = false;
        }
        for(int mi = 1; mi < n; mi++){
            match[mi][0] = false;
        }
        // dp
        for(int mi = 1; mi < n; mi++){
            for(int mj = 1; mj < m; mj++){
                int i = mi - 1;
                int j = mj - 1;
                if(p.charAt(j) == s.charAt(i) || p.charAt(j) == '.'){
                    match[mi][mj] = match[mi-1][mj-1];
                }
                else if(p.charAt(j) == '*'){
                    if(p.charAt(j-1) == s.charAt(i) || p.charAt(j-1) == '.'){
                        match[mi][mj] = match[mi-1][mj] || match[mi][mj-2];
                    }
                    else{
                        match[mi][mj] = match[mi][mj-2];
                    }
                }
                else{
                    match[mi][mj] = false;
                }
            }
        }
        return match[n-1][m-1];
    }
}
C语言的文件读取与写入操作 学了一年的C语言了,现在回过头来做一下总结。并且博客开了挺长一段时间却没有谢什么实际的东西。现在做下总结,对之后的学习帮助应该挺大的。与大家共勉! 现在,我先来介绍一下C语言的文件读取与写入的原理和具体操作方法。 C语言文件读取与写入是通过将文件看成一个字符序列进行读入和写出的。所以读取与写入文件就用到了指针,而这个指针是一个特殊的指针,我们称为文件指针。 指针名称是 阅读详情

相关推荐

【问题解决】本地 Llama / Qwen 运行报错:transformers 版本不兼容、依赖冲突解决

Flask是一个轻量级的Web框架,设计上非常简单和易于扩展。它并不强制使用任何特定的项目结构或工具,这使得开发者能够根据自己的需求自由地设计应用。Flask的灵活性使它成为了许多小型项目和原型开发的理想选择。FastAPI是一个现代的Web框架,基于Python 3.7+,专为构建API而设计。FastAPI的特点是高性能、支持异步操作,并且内置了许多现代Web应用所需的功能,如数据验证和自动生成API文档等。从性能角度看,FastAPI在高并发场景下具有明显优势,特别是在I/O密集型的应用中。

博客 767

LeetCode 10. Regular Expression Matching, 正则表达式匹配 ,C#

前言 本文介绍了 LeetCode10 题 , “Regular Expression Matching”, 也就是 “正则表达式匹配” 的问题. 本文使用 C# 语言完成题目,介绍了3种方法供大家参考,分别为 分段匹配法,回溯法,动态规划法。 题目 English LeetCode 10. Regular Expression Matching Given an input string ...

wf824284257的博客 813

小米5X-miui12.5.8安卓11定制资源 解锁bl状态fast模式刷写 带root

资源说明;1-----刷写前提是手机必须解锁bl先。而且会在fast模式刷写固件2-----刷写方法与官方刷写步骤一样3-----此固件为定制初始固件。主要用于自己机型账号忘记。写入后可登陆。4-----属于适配固件。也许有个别小bug。不接受请勿下载5-----需要一定的刷机常识与动手能力的友友刷写。6-----资源有可复制性。下载后不支持退。请知悉7-----定制其他需求可以在csdn私信博主参考博文:https://mp.csdn.net/mp_blog/creation/editor?spm=1001.2014.3001.5352

LeetCode in Python 10. Regular Expression Matching (正则表达式匹配)

出现此类情况是因为我们不想选择p[j - 1],即p[j - 1]与对应位置的s[i - 1]不相等(需要注意的是这里的p[j - 1]不等于s[i - 1]包含两种情况,一是两者均为小写字母且不等,二是p[j - 1]不为‘.’)的小写字母或字符‘.’,则直接与s中对应位置比较即可,若相同则dp[i][j] == dp[i - 1][j - 1],这里需要注意字符‘.’可匹配任意字符,可归为p[i][j]==s[i][i]这类情况。出现此种情况是因为p[j - 1]与对应位置的s[i - 1]相等。

m0_45175452的博客 1620

Leetcode10. Regular Expression Matching

考虑初始条件,空字符串可以和空模式匹配,而非空字符串无法和空模式匹配,所以。不合法,我们也应该返回false。一下子是看不出来的,需要在递推的过程中算出来。,只是为了将不合法的情形包含进去);出现,也就是那个整体匹配的一部分是。末字符匹配完之后,前面有可能还有。是个合法的模式串,应该有如果。如果这个整体按照出现大于等于。,这个整体匹配完之后,还需要。,因为那个整体是匹配大于等于。可以匹配其之前的字符重复。次来匹配,那么就要求或者。可以匹配任何单个字符,次来匹配,那么结果就是。如果这个整体按照出现。

数学、算法爱好者的博客 356

LeetCode 10. Regular Expression Matching【正则表达式匹配】

文章目录题目描述结果记忆化搜索结果动态规划结果我的记忆化搜索代码网络的题目解析解法一:递归暴力求解解法二:记忆化搜索解法三:动态规划反思参考资料 题目描述 结果 记忆化搜索结果 动态规划结果 我的记忆化搜索代码 我是看了很多个测试用例才过的题,以后要改改,不能看测试用例了,机试的时候才不会告诉你测试用例呢!需要警醒!!! //我的想法是记忆化搜索 //首先是DFS,然后DFS里面引入记忆...

yc_cy1999的博客 509

Leetcode——Regular Expression Matching

题目描述 Given an input string (s) and a pattern §, implement regular expression matching with support for ‘.’ and ‘*’. 给定一个字符串s和匹配的模板p,使用“.”和“*”,实施正则表达式的匹配 ‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element. The matching shoul

Blackoutdragon的博客 451

leetcode题解:第10Regular Expression Matching

https://leetcode-cn.com/problems/regular-expression-matching/ 分析 这道题的难点在于存在a*这样的组合,如何处理与这种组合匹配的字符数是很棘手的问题,例如: s = "aaa", p = "aa*" s = "baaa", p = "ba*" 第一个例子中a*匹配2个字符,第二个例子中a*匹配3个字符。可以看出,匹配的字符数不仅与s有关,还与p中组合前面的字符有关(后面也有可能)。如果是.*这样的组合,会更加麻烦。 我一开始的想法是用双指针分别

chenf1999的博客 311

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

题目:Regular Expression Matching 正则表达式匹配 难度:困难 Given an input string (s) and a pattern (p), implement regular expression matching with support for'.'and'*'. '.' Matches any single character. '*' ...

qq_21963133的博客 547

[Leetcode] 10. Regular Expression Matching 解题报告

题目: 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

魔豆(Magicbean)的博客 1690

LeetCode10. Regular Expression Matching解题报告(Python)

题目分析: 这道题目是实现简单的正则表达式,需要实现'.'与'*',其中'.'可表示任何字符,'*'表示前一个字符出现0或任意次。首先想到贪心穷举的方法去解决但是考虑到类似'.*'这种可以变成任意串的东西很难穷举。考虑其他方法,其中递归便是一种,将问题分为子问题去考虑。一般情况下递归可转换为动态规划动态规划可以用空间换取时间,由于递归时间长可能会时间超限我们也应该考虑使用动态规划解决。 递归...

L141210113的专栏 2216

leetcode10题——***Regular Expression Matching

题目 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

buptlrw的专栏 2600

[LeetCode - 动态规划] 10. Regular Expression Matching

1 问题 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 inpu

TecLand 1024

Leetcode: Regular Expression Matching

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 st

ZkvIA的博客 2万+

LeetCode10. Regular Expression Matching动态规划&递归】

       对于p字符串有点、字母、点*、字母*四种元素,点匹配任意一个字母,字母匹配相同的一个字母,点*匹配任意字母(可以是任意不同字母,例如.*匹配abc),字母*匹配连续任意个相同字母,值得注意的是*的任意包括0个。由于*可以匹配任意个,造成检验s和p是否完全匹配的时候难以确定究竟*匹配几个字母合适,这正是本题的关键点。题意简单粗暴,看一下原题,然后分析一下如何处理。 Given an...

zhaoqinmuxue的博客 1856

LeetCode Regular Expression Matching

简易版正则表达式匹配,只有两种通配符,”.”表示任意一个字符,”c*”表示字符c可以有零个或多个。

DRFish 2029

LeetCode10.Regular Expression Matching(Python)

Problem LeetCode 10: 正则表达式匹配 难度:hard 给定输入字符串和模式(P),实现与“.”和“”支持匹配的正则表达式。 “.”匹配任何单个字符。 “”与前面的单个元素零个或多个匹配。 匹配应该覆盖整个输入字符串(而不是部分)。 注: s可以为空,并且只包含小写字母a-z。 p可以为空,只包含小写字母a-z和类似的字符。或者*。 例1: 输入:S=“AA”, P=“A” 输出...

E.W的博客 1444

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

itachi0的专栏 346

leetcode 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 el...

scqlovezy的博客 275

LeetCode Top 100 高频算法题:10 Regular Expression Matching

LeetCode Top 100高频算法题,即LeetCode上最高频的100道求职面试算法题。小编和实验室同学之前面试找工作,也只刷了剑指offer和这top 100算法题,在实际面试中也遇到了很多LeetCode上的原题。剑指offer算法最优解之前和大家分享了,LeetCode Top 100这100道算法题,每道题小编都刷了很多遍,并且总结了一种最适合面试时手撕算法的最优解法。后续每天和大家分享一道LeetCode top 100高频算法题,以及小编总结的最优解法。 下面是第005是道算法题:

我的博客 733
上一篇: 【LeetCode】解题5:Longest Palindromic Substring(多解法:动态规划+中心扩散)
下一篇: 【LeetCode】解题32:Longest Valid Parentheses(多解法:栈+动态规划+计数器)
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值