当前位置:网站首页>[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);
}
}
边栏推荐
猜你喜欢

First experience of Galaxy Kirin

SOC_SD_CLK

1.8 - 多级存储
![[Hot100]回文子串 与 最长回文子串](/img/a5/10dec640f02023c4d55cb42e6309fb.png)
[Hot100]回文子串 与 最长回文子串

1.5 - logical operation

Principle: webmvcconfigurer and webmvcconfigurationsupport pit avoidance Guide

RT thread Kernel Implementation (II): critical area, object container

Porting RT thread to s5p4418 (V): thread communication
![[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network](/img/39/c9bf235828b6d50e7931876fa4a918.png)
[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network

Steps for formulating class or file templates in idea
随机推荐
1.5 - logical operation
ROS system problem: rosdep init
It turns out that you are such an array. You have finally learned
我开户后把账号忘记了咋办?股票在网上开户安全吗?
Wuenda coursera deep learning course
tomorrow! "Mobile cloud Cup" competition air publicity will start!
Huawei full-scale Daniel shared the 598 page full-color Manual of network protocols for the first time
1.2 (supplementary)
MySQL优化:从十几秒优化到三百毫秒
Practice summary of Prometheus project in amu Laboratory
元宇宙由哪些底层技术支撑?
1.2(补充)
【我的创作纪念日】一周年随笔
1.4 - 定点数与浮点数
MySQL中的InnoDB引擎
1.9 - Classification of memory
memcpy内存重叠的解决
Deep learning --- the weight of the three good students' scores (3)
六,购物⻋与订单
Getting started with research