当前位置:网站首页>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;
}
边栏推荐
- IVR在voip电话系统的应用与价值
- Common modules in ansible
- Nacos introduction and deployment
- Why is the value represented by a negative number greater than an integer by 1?
- Full analysis of domain name resolution process means better text understanding
- 攻防世界--easy_web
- 517. 超级洗衣机
- Week 6 Learning Representation: Word Embedding (symbolic →numeric)
- MySQL master-slave synchronization and master-slave synchronization delay solution
- 推荐必读:测试人员如何快速熟悉新业务?
猜你喜欢

Security permission management details

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

Shell的read 读取控制台输入、read的使用

动态内存管理及柔性数组

Simulation of future air pollution changes

SSTI-payload和各种绕过方法

An online accident, I suddenly realized the essence of asynchrony

开发转测试:从零开始的6年自动化之路

安装NCCL\mpirun\horovod\nvidia-tensorflow(3090Ti)

NetCore MySql The user specified as a definer (‘admin‘@‘%‘) does not exist
随机推荐
[pytorch] install torch 1.8.1 and check whether torch version and GPU are available
Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0
SQL注入
安装NCCL\mpirun\horovod\nvidia-tensorflow(3090Ti)
Getting started with ALV
Excel VBA: summarize calculation output results by date (SUMIF)
Leetcode linked list problem - 206. reverse linked list (learn linked list by one question and one article)
怎么办理聚合收款码
flex布局原理及常见的父项元素
If MySQL calculates the current month change / current month increase / year-on-year change / year-on-year increase?
Security permission management details
【个人总结】2022.7.24周结
[acwing] 1268. Simple questions
LeetCode链表问题——206.反转链表(一题一文学会链表)
Go-Excelize API源码阅读(六)—— DeleteSheet(sheet string)
NetCore MySql The user specified as a definer (‘admin‘@‘%‘) does not exist
Your understanding of the "happen before principle" may be wrong?
家居vr全景展示制作提高客户转化
Embedded development notes, practical knowledge sharing
The first positive number missing in question 41 of C language. Two methods, preprocessing, fast sorting and in situ hashing