当前位置:网站首页>[Hot100]10. Regular Expression Matching
[Hot100]10. Regular Expression Matching
2022-06-30 06:53:00 【Wang Liuliu's it daily】
10. Regular Expression Matching
Difficult questions
Dynamic programming :
dp[i][j] Express s Before i Can the p Before j A match
● Transfer equation : We need to pay attention to , because dp[0][0] Represents the status of empty characters , therefore dp[i][j] The corresponding added character is s[i - 1] and p[j - 1] .
○ When p[j - 1] = ‘*’ when , dp[i][j] In the case of true When is equal to the true :
■ dp[i][j-2]
● Combine characters p[j - 2] * As if 0 When the time , Can it match ;
■ dp[i-1][j]
● And p[j - 2] = s[i - 1]: Instant character p[j - 2] There are more 1 When the time , Can it match ;
● And p[j - 2] = ‘.’: Instant character ‘.’ There are more 1 When the time , Can it match ;
○ When p[j - 1] != ‘*’ when , dp[i][j] In the case of true When is equal to the true :
■ dp[i - 1][j - 1]
● And s[i - 1] = p[j - 1]: Instant character p[j - 1] When it occurs more than once , Can it match ;
● And p[j - 1] = ‘.’: I'm going to change the character . Treat as character s[i - 1] when , Can it match ;
class Solution {
public boolean isMatch(String s, String p) {
int m = s.length() + 1, n = p.length() + 1;
boolean[][] dp = new boolean[m][n];
dp[0][0] = true;
// Initialize first line
for(int j = 2; j < n; j += 2)
dp[0][j] = dp[0][j - 2] && p.charAt(j - 1) == '*';
// State shift
for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
if(p.charAt(j - 1) == '*') {
if(dp[i][j - 2]) dp[i][j] = true;
// 1.
else if(dp[i - 1][j] && s.charAt(i - 1) == p.charAt(j - 2)) dp[i][j] = true; // 2.
else if(dp[i - 1][j] && p.charAt(j - 2) == '.') dp[i][j] = true; // 3.
} else {
if(dp[i - 1][j - 1] && s.charAt(i - 1) == p.charAt(j - 1)) dp[i][j] = true; // 1.
else if(dp[i - 1][j - 1] && p.charAt(j - 1) == '.') dp[i][j] = true; // 2.
}
}
}
return dp[m - 1][n - 1];
}
}
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);
}
}
边栏推荐
- 1.4 - 定点数与浮点数
- Force buckle ------ replace blank space
- RT thread Kernel Implementation (I): threads and scheduling
- Fastapi learning Day2
- 判断h5在两端是在微信环境还是企业微信环境
- Hao looking for a job
- tomorrow! "Mobile cloud Cup" competition air publicity will start!
- 【我的OpenGL学习进阶之旅】关于OpenGL的着色器的向量和矩阵分类的访问方式: xyzw/rgba/stpq以及数组下标
- The most complete sentence in history
- [Hot100]10. 正则表达式匹配
猜你喜欢

The most complete sentence in history

基础刷题(一)

随机网络,无标度网络,小世界网络以及NS小世界的性能对比matlab仿真

0基础转行软件测试,如何实现月薪9.5k+

Definition and use of ROS topic messages

0 basic job transfer software test, how to achieve a monthly salary of 9.5k+

明天!“移动云杯”大赛空宣会开播!

Why does ETL often become ELT or even let?

关注这场直播,了解能源行业双碳目标实现路径

1.4 - fixed and floating point numbers
随机推荐
freemarker
1.2(补充)
Cmake post makefile:32: * * * missing separator Stop.
【模糊神经网络】基于模糊神经网络的移动机器人路径规划
RT thread migration to s5p4418 (III): static memory pool management
记录一次腾讯测试开发工程师自动化接口测试实践经验
RT thread Kernel Implementation (IV): multi priority
Browser downloads files as attachments
Force buckle ------ replace blank space
Pycharm shortcut key
Use of sscanf function
Introduction to neural networks
c# - C#用fo-dicom对CT图像的PixelData进行处理和转换
InnoDB engine in MySQL
1.7 - CPU performance indicators
史上最全一句话木马
leetcode:98. 验证二叉搜索树
【转】存储器结构、cache、DMA架构分析
C # - C # process and convert pixeldata of CT images with fo DICOM
Imxq Freescale yocto project compilation record