当前位置:网站首页>[leetcode] 10. Regular expression matching
[leetcode] 10. Regular expression matching
2022-06-24 14:11:00 【Xiaoqu】
10、 Regular Expression Matching
subject :
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 :
Words , This topic , Really? , Everyone understands it differently .
such as : The meaning of matching zero or more preceding elements : It can be understood as : * and * front ( Set to x) As one ; perhaps * Elements before and * They are independent . These two understandings , Nothing wrong. .
The title is not difficult. , But the expression is really stretching .
Put aside the question of understanding , The best solution to this problem is to use state machine transfer .
The code is as follows :
Life is too short , I use java.
class Solution {
public boolean isMatch(String s, String p) {
return s.matches(p);
}
}

Ha ha ha , Okay , No more noise , Get down to business .
Normal code :
class Solution {
public boolean isMatch(String s, String p) {
int m = s.length();
int n = p.length();
boolean[][] f = new boolean[m + 1][n + 1];
f[0][0] = true;
for (int i = 0; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (p.charAt(j - 1) == '*') {
f[i][j] = f[i][j - 2];
if (matches(s, p, i, j - 1)) {
f[i][j] = f[i][j] || f[i - 1][j];
}
} else {
if (matches(s, p, i, j)) {
f[i][j] = f[i - 1][j - 1];
}
}
}
}
return f[m][n];
}
public boolean matches(String s, String p, int i, int j) {
if (i == 0) {
return false;
}
if (p.charAt(j - 1) == '.') {
return true;
}
return s.charAt(i - 1) == p.charAt(j - 1);
}
}
边栏推荐
- A review of text contrastive learning
- HarmonyOS.2
- 在线文本实体抽取能力,助力应用解析海量文本数据
- 4个不可不知的“安全左移”的理由
- Jerry's seamless looping [chapter]
- The project manager needs to look at six characteristics to build a team
- Jupiter notebook operation
- 杰理之TIMER0 用默认的 PA13 来检测脉宽【篇】
- `Thymeleaf`模板引擎全面解析
- Zhiyuan community weekly 86: Gary Marcus talks about three linguistic factors that can be used for reference in large model research; Google puts forward the Wensheng graph model parti which is compar
猜你喜欢
![根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]](/img/f7/8d026c0e4435fc8fd7a63616b4554d.png)
根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]

SAP Marketing Cloud 功能概述(四)

杰理之TIMER0 用默认的 PA13 来检测脉宽【篇】

MIT-6.824-lab4A-2022(万字讲解-代码构建)
![[R language data science] (XIV): random variables and basic statistics](/img/87/3606041a588ecc615eb8013cdf9fb1.png)
[R language data science] (XIV): random variables and basic statistics

How to solve the problem that iterative semi supervised training is difficult to implement in ASR training? RTC dev Meetup

box-sizing

融云通信“三板斧”,“砍”到了银行的心坎上

Rasa 3.x 学习系列-非常荣幸成为 Rasa contributors 源码贡献者,和全世界的Rasa源码贡献者共建共享Rasa社区!

居家办公更要高效-自动化办公完美提升摸鱼时间 | 社区征文
随机推荐
2022 construction elevator driver (construction special type of work) examination questions and online simulation examination
OpenHarmony 1
杰理之增加一个输入捕捉通道【篇】
百度地图API绘制点及提示信息
Second, the examinee must see | consolidate the preferred question bank to help the examinee make the final dash
Three efficient programming skills of go language
Kotlin composite suspend function
box-sizing
Baidu map API drawing points and tips
HarmonyOS.2
The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance
杰理之无缝循环播放【篇】
4 reasons for "safe left shift"
Jerry has opened a variety of decoding formats, and the waiting time from card insertion to playback is long [chapter]
数商云:加强供应商管理,助推航空运输企业与供应商高效协同
unity 等高线创建方法
Jericho turns on shouting in all modes to increase mic automatic mute [chapter]
SaaS management system solution of smart Park: enabling the park to realize information and digital management
Télétravail: Camping à la maison gadgets de bureau | rédaction communautaire
Digital business cloud: strengthen supplier management and promote efficient collaboration between air transport enterprises and suppliers