当前位置:网站首页>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/
边栏推荐
- Nacos
- 家居网购项目
- torch. histc
- LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
- C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
- Leetcode:829. Sum of continuous integers
- Avalanche problem and the use of sentinel
- 【伸手党福利】JSONObject转String保留空字段
- 过滤器 Filter
- Explain spark operation mode in detail (local+standalone+yarn)
猜你喜欢
![[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation](/img/b3/887d3fb64acbf3702814d32e2e6414.png)
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation

实现pow(x,n)函数

完全背包问题

衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)

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

LeetCode 128最长连续序列(哈希set)

Appium自动化测试基础--补充:C/S架构和B/S架构说明

后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动

pytorch训练深度学习网络设置cuda指定的GPU可见

还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
随机推荐
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
Ctfshow blasting WP
过滤器 Filter
使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
Thread data sharing and security -threadlocal
Finally in promise
Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
Feature pyramid networks for object detection
Design of serial port receiving data scheme
5、【WebGIS实战】软件操作篇——服务发布及权限管理
Filter
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
TEC: Knowledge Graph Embedding with Triple Context
不用加减乘除实现加法
Promise中finally的用法
Feature Pyramid Networks for Object Detection论文理解
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
Feign远程调用和Getaway网关