当前位置:网站首页>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/
边栏推荐
- [deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
- Listener listener
- Finally in promise
- FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
- Home online shopping project
- Golang multi graph generation gif
- Leetcode: offer 59 - I. maximum value of sliding window
- GCC usage, makefile summary
- 详解Spark运行模式(local+standalone+yarn)
- Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
猜你喜欢

监听器 Listener

不用加减乘除实现加法

Home online shopping project

Implement pow (x, n) function

访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载

快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内

The difference between MFC for static libraries and MFC for shared libraries

Error: plug ins declaring extensions or extension points must set the singleton directive to true

Test function in pychram

在线公网安备案保姆级教程【伸手党福利】
随机推荐
MFC窗口滚动条用法
线程数据共享和安全 -ThreadLocal
Cookie&Session
Bilinear upsampling and f.upsample in pytorch_ bilinear
IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
leetcode 1482 猜猜看啊,这道题目怎么二分?
Error: plug ins declaring extensions or extension points must set the singleton directive to true
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
md5sum操作
Implement pow (x, n) function
Ridge regression and lasso regression
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
静态库使用MFC和共享库使用MFC的区别
Online public network security case nanny level tutorial [reaching out for Party welfare]
【伸手党福利】开发人员重装系统顺序
Leetcode 1482 guess, how about this question?
Leetcode:829. Sum of continuous integers
10、Scanner. Next() cannot read spaces /indexof -1
Binary tree god level traversal: Morris traversal