当前位置:网站首页>10. regular expression matching
10. regular expression matching
2022-07-01 03:43:00 【Sun_ Sky_ Sea】
10. Regular Expression Matching
Original title link :https://leetcode.cn/problems/regular-expression-matching/
Give you a string s And a character rule p, Please come to realize a support ‘.’ and ‘*’ Regular expression matching .
‘.’ Match any single character
‘*’ Match zero or more previous elements
Match , Is to cover Whole character string s Of , Instead of partial strings .
Example 1:
Input :s = “aa”, p = “a”
Output :false
explain :“a” Can't match “aa” Whole string .
Example 2:
Input :s = “aa”, p = “a*”
Output :true
explain : because ‘*’ Represents the element that can match zero or more preceding elements , The element in front of here is ‘a’. therefore , character string “aa” Can be regarded as ‘a’ I repeated it once .
Example 3:
Input :s = “ab”, p = “."
Output :true
explain :".” Means zero or more can be matched (‘*’) Any character (‘.’).
Tips :
1 <= s.length <= 20
1 <= p.length <= 30
s Only from a-z Lowercase letters of .
p Only from a-z Lowercase letters of , As well as the character . and *.
Ensure that characters appear every time * when , All of them are matched with valid characters
Their thinking :
The problem of dynamic programming , State needs to be defined , Initialization status , Find the state transition equation , See the references for details .
Code implementation :
class Solution:
def isMatch(self, s: str, p: str) -> bool:
# Definition dp[i][j]: s Before i Characters and p Before j Whether characters match
# So the value is True perhaps False
m, n = len(s), len(p)
# One more row, one more column , Represents an empty string s and p
dp = [[False] * (n + 1) for _ in range(m + 1)]
# assignment dp The border
# Space transfer energy matching empty string , So it is True
dp[0][0] = True
for j in range(1, n + 1):
# p[j - 1] Can match 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):
# If the current character matches or p The current character of is a universal character '.'
if s[i - 1] == p[j - 1] or p[j - 1] == '.':
# Whether the current character matches or not is the same as looking forward to a character match
dp[i][j] = dp[i - 1][j - 1]
# If p The current character of is '*'
elif p[j - 1] == '*':
# If s The current character of does not match p Is equal to the previous character of the current character of
# also p The previous character of the current character of is not a universal character '.'
if s[i - 1] != p[j - 2] and p[j - 2] != '.':
# When it matches and goes p The first two characters are the same
dp[i][j] = dp[i][j - 2]
else:
dp[i][j] = dp[i][j-2] | dp[i-1][j]
return dp[m][n]
reference :
https://leetcode.cn/problems/regular-expression-matching/solution/by-flix-musv/
边栏推荐
- torch. histc
- LeetCode 128最长连续序列(哈希set)
- Leetcode:829. 连续整数求和
- Feature pyramid networks for object detection
- 72. 编辑距离
- 排序链表(归并排序)
- 166. 分数到小数
- Use of comment keyword in database
- Appium fundamentals of automated testing - basic principles of appium
- Learning notes for introduction to C language multithreaded programming
猜你喜欢
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
pytorch nn. AdaptiveAvgPool2d(1)
【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
Cookie&Session
整合阿里云短信的问题:无法从静态上下文中引用非静态方法
详解Spark运行模式(local+standalone+yarn)
LeetCode 128最长连续序列(哈希set)
用小程序的技术优势发展产业互联网
Online public network security case nanny level tutorial [reaching out for Party welfare]
Edge drawing: a combined real-time edge and segment detector
随机推荐
Addition without addition, subtraction, multiplication and division
SEM of C language_ Tvariable type
How keil displays Chinese annotations (simple with pictures)
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
【伸手党福利】开发人员重装系统顺序
FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)
Leetcode:829. Sum of continuous integers
[TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
How do I use Google Chrome 11's Upload Folder feature in my own code?
Thread data sharing and security -threadlocal
Pyramid Scene Parsing Network【PSPNet】论文阅读
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
Asgnet paper and code interpretation 2
Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
Valid brackets (force deduction 20)
Golang multi graph generation gif
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
Future of NTF and trends in 2022
Explain spark operation mode in detail (local+standalone+yarn)
【TA-霜狼_may-《百人计划》】2.1 色彩空间