当前位置:网站首页>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/
边栏推荐
- 【TA-霜狼_may-《百人计划》】2.1 色彩空间
- [TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions
- 深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
- Unexpected token o in JSON at position 1 ,JSON解析问题
- [daily training] 1175 Prime permutation
- 在线公网安备案保姆级教程【伸手党福利】
- 241. 为运算表达式设计优先级
- Include() of array
- 二叉树神级遍历:Morris遍历
- The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
猜你喜欢
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
二叉树神级遍历:Morris遍历
[TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
Online public network security case nanny level tutorial [reaching out for Party welfare]
Unexpected token o in JSON at position 1 ,JSON解析问题
[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
TEC: Knowledge Graph Embedding with Triple Context
深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
随机推荐
谷粒学院微信扫码登录过程记录以及bug解决
8. string conversion integer (ATOI)
Test function in pychram
pytorch nn.AdaptiveAvgPool2d(1)
快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
Develop industrial Internet with the technical advantages of small programs
LeetCode 128最长连续序列(哈希set)
5. [WebGIS practice] software operation - service release and permission management
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
[nine day training] content III of the problem solution of leetcode question brushing Report
ECMAScript 6.0
MFC窗口滚动条用法
Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
实现pow(x,n)函数
Error: plug ins declaring extensions or extension points must set the singleton directive to true
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
Leetcode: offer 59 - I. maximum value of sliding window
242. 有效的字母异位词
SEM of C language_ Tvariable type