当前位置:网站首页>10. 正则表达式匹配
10. 正则表达式匹配
2022-07-01 03:23:00 【Sun_Sky_Sea】
10. 正则表达式匹配
原始题目链接:https://leetcode.cn/problems/regular-expression-matching/
给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 ‘.’ 和 ‘*’ 的正则表达式匹配。
‘.’ 匹配任意单个字符
‘*’ 匹配零个或多个前面的那一个元素
所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。
示例 1:
输入:s = “aa”, p = “a”
输出:false
解释:“a” 无法匹配 “aa” 整个字符串。
示例 2:
输入:s = “aa”, p = “a*”
输出:true
解释:因为 ‘*’ 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 ‘a’。因此,字符串 “aa” 可被视为 ‘a’ 重复了一次。
示例 3:
输入:s = “ab”, p = “."
输出:true
解释:".” 表示可匹配零个或多个(‘*’)任意字符(‘.’)。
提示:
1 <= s.length <= 20
1 <= p.length <= 30
s 只包含从 a-z 的小写字母。
p 只包含从 a-z 的小写字母,以及字符 . 和 *。
保证每次出现字符 * 时,前面都匹配到有效的字符
解题思路:
动态规划的问题,需要定义状态,初始化状态,找状态转移方程,具体看参考文献。
代码实现:
class Solution:
def isMatch(self, s: str, p: str) -> bool:
# 定义dp[i][j]: s的前i个字符和p的前j个字符是否匹配
# 所以值是True 或者 False
m, n = len(s), len(p)
# 多一行一列,代表空的字符串s和p
dp = [[False] * (n + 1) for _ in range(m + 1)]
# 赋值dp边界
# 空传能匹配空串,所以是True
dp[0][0] = True
for j in range(1, n + 1):
# p[j - 1]可以匹配p[j - 2]
if p[j - 1] == '*':
dp[0][j] = dp[0][j - 2]
for i in range(1, m + 1):
for j in range(1, n + 1):
# 如果当前字符匹配或者p的当前字符是万能字符'.'
if s[i - 1] == p[j - 1] or p[j - 1] == '.':
# 当前是否匹配和都往前看一个字符匹配是一样的
dp[i][j] = dp[i - 1][j - 1]
# 如果p的当前字符是'*'
elif p[j - 1] == '*':
# 如果s的当前字符不和p的当前字符的前一个字符相等
# 并且p的当前字符的前一个字符不是万能字符'.'
if s[i - 1] != p[j - 2] and p[j - 2] != '.':
# 当是否匹配和往p的前看两个字符是一样的
dp[i][j] = dp[i][j - 2]
else:
dp[i][j] = dp[i][j-2] | dp[i-1][j]
return dp[m][n]
参考文献:
https://leetcode.cn/problems/regular-expression-matching/solution/by-flix-musv/
边栏推荐
- leetcode 1482 猜猜看啊,这道题目怎么二分?
- Appium自动化测试基础 — APPium基本原理
- Cygwin的下载和安装配置
- 数据库中COMMENT关键字的使用
- 静态库使用MFC和共享库使用MFC的区别
- Pyramid scene parsing network [pspnet] thesis reading
- Bilinear upsampling and f.upsample in pytorch_ bilinear
- [deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
- How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
- Leetcode 1482 guess, how about this question?
猜你喜欢

Data exchange JSON

Appium自动化测试基础--补充:C/S架构和B/S架构说明
![5. [WebGIS practice] software operation - service release and permission management](/img/5d/070e207bd96e60ba1846d644d4fb54.png)
5. [WebGIS practice] software operation - service release and permission management

数据库中COMMENT关键字的使用

排序链表(归并排序)

Nacos

Use of comment keyword in database

idea插件备份表
![[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st](/img/9f/187ca83be1b88630a6c6fbfb0620ed.png)
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st

Appium自动化测试基础 — APPium基本原理
随机推荐
Edge drawing: a combined real-time edge and segment detector
E15 solution for cx5120 controlling Huichuan is620n servo error
Addition without addition, subtraction, multiplication and division
Ouc2021 autumn - Software Engineering - end of term (recall version)
数据交换 JSON
Cookie&Session
Pathmeasure implements loading animation
pytorch nn. AdaptiveAvgPool2d(1)
数组的includes( )
AfxMessageBox和MessageBox的用法
Explain spark operation mode in detail (local+standalone+yarn)
TEC: Knowledge Graph Embedding with Triple Context
Edlines: a real time line segment detector with a false detection control
Detailed explanation of ES6 deconstruction grammar
不用加减乘除实现加法
[nine day training] content III of the problem solution of leetcode question brushing Report
Database DDL (data definition language) knowledge points
Golang multi graph generation gif
Pyramid Scene Parsing Network【PSPNet】论文阅读
Test function in pychram