当前位置:网站首页>[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);
}
}
边栏推荐
- Operation of simulated examination platform of examination question bank for B certificate of Jiangxi provincial safety officer in 2022
- Jerry's seamless looping [chapter]
- 文本对比学习综述
- 项目经理搭建团队,需要看6个特征
- v-for 中 key的作用和原理
- 专精特新“小巨人”再启动,“企业上云”数字赋能
- Jericho After sleep, the system will wake up regularly and continue to run without resetting [chapter]
- JS remove string spaces
- Promotion of Project Manager
- AntD checkbox,限制选中数量
猜你喜欢

Tupu software is the digital twin of offshore wind power, striving to be the first

Three efficient programming skills of go language

Research on MySQL composite index

box-sizing

【无标题】

远程办公之:在家露营办公小工具| 社区征文
![[R language data science] (XIV): random variables and basic statistics](/img/87/3606041a588ecc615eb8013cdf9fb1.png)
[R language data science] (XIV): random variables and basic statistics

Puzzle (016.2) finger painting Galaxy

Baidu map API drawing points and tips
![Maximum path sum in binary tree [handle any subtree, then handle the whole tree]](/img/d0/91ab1cc1851d7137a1cab3cf458302.png)
Maximum path sum in binary tree [handle any subtree, then handle the whole tree]
随机推荐
Kotlin coordination channel
kotlin 协程通道
Go语言三个高效编程的技巧
PM也要学会每天反省
2022质量员-设备方向-通用基础(质量员)考试题及答案
SaaS management system solution of smart Park: enabling the park to realize information and digital management
The real project managers are all closed-loop masters!
One click to generate University, major and even admission probability. Is it so magical for AI to fill in volunteer cards?
pip uninstall all packages except builtin package
CONDA and pip commands
Maximum path sum in binary tree [handle any subtree, then handle the whole tree]
Rasa 3.x 学习系列-非常荣幸成为 Rasa contributors 源码贡献者,和全世界的Rasa源码贡献者共建共享Rasa社区!
PM should also learn to reflect every day
OpenHarmony 1
Jerry's infrared filtering [chapter]
Jerry added an input capture channel [chapter]
AutoRF:从单视角观察中学习3D物体辐射场(CVPR 2022)
[R language data science] (XIV): random variables and basic statistics
Operation of simulated examination platform for examination questions of coal production and operation units (safety production management personnel) in 2022
Tupu software is the digital twin of offshore wind power, striving to be the first