当前位置:网站首页>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/
边栏推荐
- FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
- FCN全卷积网络理解及代码实现(来自pytorch官方实现)
- LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
- shell脚本使用两个横杠接收外部参数
- 【伸手党福利】JSONObject转String保留空字段
- Force buckle - sum of two numbers
- 10、Scanner.next() 无法读取空格/indexOf -1
- Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
- Appium自动化测试基础 — APPium基本原理
- Leetcode: offer 59 - I. maximum value of sliding window
猜你喜欢

Use of comment keyword in database

Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)

复习专栏之---消息队列

LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)

使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况

Learning notes for introduction to C language multithreaded programming

Addition without addition, subtraction, multiplication and division

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

5、【WebGIS实战】软件操作篇——服务发布及权限管理

IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does
随机推荐
Learning notes for introduction to C language multithreaded programming
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
leetcode 1818 绝对值,排序,二分法,最大值
Appium自动化测试基础 — APPium基本原理
Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page
Listener listener
ASGNet论文和代码解读2
Download and installation configuration of cygwin
访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
You cannot right-click F12 to view the source code solution on the web page
LeetCode 128最长连续序列(哈希set)
在线公网安备案保姆级教程【伸手党福利】
Data exchange JSON
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
pytorch nn. AdaptiveAvgPool2d(1)
Leetcode:829. Sum of continuous integers
TEC: Knowledge Graph Embedding with Triple Context
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
数据交换 JSON
5. [WebGIS practice] software operation - service release and permission management