当前位置:网站首页>10. Regular expression matching
10. Regular expression matching
2022-07-26 05:21:00 【Xiao Lu wants to brush the questions】
List of articles
Preface
`
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 .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/regular-expression-matching
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking
Sample correspondence model
Define 2D table
String validity check
str Can not have . and *
exp It can't start with *. Two * Not next to
public static boolean isValid(char[] s, char[] e) {
// s There can be no '.' or '*'
for (int i = 0; i < s.length; i++) {
if (s[i] == '*' || s[i] == '.') {
return false;
}
}
// At the beginning e[0] It can't be '*', There is no adjacent '*'
for (int i = 0; i < e.length; i++) {
if (e[i] == '*' && (i == 0 || e[i - 1] == '*')) {
return false;
}
}
return true;
}
Recurrence of violence
Recursive meaning :
str from si Start and everything behind it , Can you be exp from ei Set out and all the matches behind it
It can be matched and returned true, otherwise false
ei The next position is not *
If ei+1 No * , It means that I e There is no room for operation . It can't count on the following * Make it disappear , For this moment si Must be able to communicate with ei Right up
Can only be [ei] The character is equal to [si] character
or [ei] Character is a . , It can become everythingei+1 yes *
0) Change to zero
Give Way a* change 0 individual a, Match later
1) change 1 individual a
2) change 2 individual a
3) 3 individual a
4) 4 individual a
Every branch goes , But one goes through , Go straight back to true, All branches fail , return false,
public static boolean isMatch1(String str, String exp) {
if (str == null || exp == null) {
return false;
}
char[] s = str.toCharArray();
char[] e = exp.toCharArray();
return isValid(s, e) && process(s, e, 0, 0);
}
// str[si.....] Can you be exp[ei.....] Match it out ! true false
public static boolean process(char[] s, char[] e, int si, int ei) {
if (ei == e.length) {
// exp be without str?
return si == s.length;
}
// exp[ei] And the characters
// ei + 1 The character of position , No *
if (ei + 1 == e.length || e[ei + 1] != '*') {
// ei + 1 No *
// str[si] It has to be with exp[ei] Can match !
return si != s.length && (e[ei] == s[si] || e[ei] == '.') && process(s, e, si + 1, ei + 1);
}
// exp[ei] And the characters
// ei + 1 The character of position , yes *!
while (si != s.length && (e[ei] == s[si] || e[ei] == '.')) {
if (process(s, e, si, ei + 2)) {
return true;
}
si++;
}
return process(s, e, si, ei + 2);
}
Slope optimization + Memory search

f(13, 29) Dependence

f(12, 29) Dependence
f(12, 29)=f(13,29)+f(12,31)
stay str in i Position character and str in i+ 1 This optimization exists when the position character is the same
When I am one - When a grid has enumeration behavior , I will observe the grid he has calculated , Can I replace it with a piece ,
Thus, we can get the value of this lattice one by one by using a finite number of positions
public static boolean isMatch2(String str, String exp) {
if (str == null || exp == null) {
return false;
}
char[] s = str.toCharArray();
char[] e = exp.toCharArray();
if (!isValid(s, e)) {
return false;
}
int[][] dp = new int[s.length + 1][e.length + 1];
// dp[i][j] = 0, I didn't count !
// dp[i][j] = -1 Yes , The return value is false
// dp[i][j] = 1 Yes , The return value is true
return isValid(s, e) && process2(s, e, 0, 0, dp);
}
public static boolean process2(char[] s, char[] e, int si, int ei, int[][] dp) {
if (dp[si][ei] != 0) {
return dp[si][ei] == 1;
}
boolean ans = false;
if (ei == e.length) {
ans = si == s.length;
} else {
if (ei + 1 == e.length || e[ei + 1] != '*') {
ans = si != s.length && (e[ei] == s[si] || e[ei] == '.') && process2(s, e, si + 1, ei + 1, dp);
} else {
if (si == s.length) {
// ei ei+1 *
ans = process2(s, e, si, ei + 2, dp);
} else {
// si It's not over
if (s[si] != e[ei] && e[ei] != '.') {
ans = process2(s, e, si, ei + 2, dp);
} else {
// s[si] You can talk to e[ei] Deserve to go up
ans = process2(s, e, si, ei + 2, dp) || process2(s, e, si + 1, ei, dp);
}
}
}
}
dp[si][ei] = ans ? 1 : -1;
return ans;
}
边栏推荐
- SIP账号注册的SIP软电话的使用和常见问题
- Hack The Box - Introduction to Networking Module详细讲解中文教程
- Day011 一维数组
- Leetcode linked list problem - 206. reverse linked list (learn linked list by one question and one article)
- Recommended reading: how can testers get familiar with new businesses quickly?
- MySQL basic learning
- Getaverse, a distant bridge to Web3
- Practical technology of SWAT Model in simulation of hydrology, water resources and non-point source pollution
- NetCore MySql The user specified as a definer (‘admin‘@‘%‘) does not exist
- DOM event flow event bubble event capture event delegate
猜你喜欢

DOM event flow event bubble event capture event delegate
![Meta analysis [whole process, uncertainty analysis] method based on R language and meta machine learning](/img/87/9f8353c5c9c700eaa63f66697aa44a.png)
Meta analysis [whole process, uncertainty analysis] method based on R language and meta machine learning

ALV report flow diagram

Application of remote sensing, GIS and GPS technology in hydrology, meteorology, disasters, ecology, environment and health

Princeton calculus reader 02 Chapter 1 -- composition of functions, odd and even functions, function images

Week 6 Learning Representation: Word Embedding (symbolic →numeric)

DOM事件流 事件冒泡-事件捕获-事件委托

Embedded sharing collection 21

Nacos introduction and deployment
C语言详解系列——函数的认识(3)形参,实参,嵌套调用和链式访问
随机推荐
Full analysis of domain name resolution process means better text understanding
Teach you how to use code to realize SSO single sign on
ABAP语法学习(ALV)
FPGA刷题——序列检测
Circular structure practice
使用Ansible中的playbook
Hack The Box - Introduction to Networking Module详细讲解中文教程
If MySQL calculates the current month change / current month increase / year-on-year change / year-on-year increase?
C语言-指针进阶
Week 6 Learning Representation: Word Embedding (symbolic →numeric)
如何优雅的复现YOLOv5官方历程(二)——标注并训练自己的数据集
DOM event flow event bubble event capture event delegate
Leetcode linked list problem - 206. reverse linked list (learn linked list by one question and one article)
攻防世界-FlatScience
攻防世界--easy_web
Yuancosmos provides a digital social platform for fashion design display
Meta analysis [whole process, uncertainty analysis] method based on R language and meta machine learning
JVM Lecture 5: how to deal with peak push of vertical and horizontal data
没背景、没学历?专科测试员进入互联网大厂是不是真的没希望?
Ansible中常用的模块