当前位置:网站首页>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/
边栏推荐
- Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
- 深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
- Valid brackets (force deduction 20)
- Unexpected token o in JSON at position 1 ,JSON解析问题
- 392. 判断子序列
- 排序链表(归并排序)
- 数据库中COMMENT关键字的使用
- Blueprism registration, download and install -rpa Chapter 1
- Explain spark operation mode in detail (local+standalone+yarn)
- 8. string conversion integer (ATOI)
猜你喜欢

Binary tree god level traversal: Morris traversal

FCN全卷积网络理解及代码实现(来自pytorch官方实现)

Appium自动化测试基础 — APPium基本原理

实现pow(x,n)函数

jeecgboot输出日志,@Slf4j的使用方法

Develop industrial Internet with the technical advantages of small programs

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

Gorilla/mux framework (RK boot): RPC error code design

Leetcode 128 longest continuous sequence (hash set)

访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
随机推荐
后台系统右边内容如何出现滚动条和解决双滚动条的问题
Sort linked list (merge sort)
214. minimum palindrome string
【伸手党福利】JSONObject转String保留空字段
Review column - message queue
Leetcode: offer 59 - I. maximum value of sliding window
pytorch nn. AdaptiveAvgPool2d(1)
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
Appium自动化测试基础--补充:C/S架构和B/S架构说明
Blueprism registration, download and install -rpa Chapter 1
[daily training] 1175 Prime permutation
BluePrism注册下载并安装-RPA第一章
[ta - Frost Wolf May - 100 people plan] 2.3 Introduction aux fonctions communes
318. 最大单词长度乘积
166. 分数到小数
4、【WebGIS实战】软件操作篇——数据导入及处理
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
JS daily development tips (continuous update)
409. 最长回文串
How do I use Google Chrome 11's Upload Folder feature in my own code?