当前位置:网站首页>[Hot100]10. 正则表达式匹配
[Hot100]10. 正则表达式匹配
2022-06-30 06:47:00 【王六六的IT日常】
10. 正则表达式匹配
困难题
动态规划:
dp[i][j] 表示 s 的前 i 个是否能被 p 的前 j 个匹配
● 转移方程: 需要注意,由于 dp[0][0] 代表的是空字符的状态, 因此 dp[i][j] 对应的添加字符是 s[i - 1] 和 p[j - 1] 。
○ 当 p[j - 1] = ‘*’ 时, dp[i][j] 在当以下任一情况为 true时等于true :
■ dp[i][j-2]
● 将字符组合 p[j - 2] * 看作出现 0 次时,能否匹配;
■ dp[i-1][j]
● 且 p[j - 2] = s[i - 1]: 即让字符 p[j - 2]多出现 1 次时,能否匹配;
● 且 p[j - 2] = ‘.’:即让字符 ‘.’ 多出现 1 次时,能否匹配;
○ 当 p[j - 1] != ‘*’ 时, dp[i][j] 在当以下任一情况为true 时等于 true :
■ dp[i - 1][j - 1]
● 且 s[i - 1] = p[j - 1]: 即让字符 p[j - 1] 多出现一次时,能否匹配;
● 且 p[j - 1] = ‘.’: 即将字符 . 看作字符 s[i - 1] 时,能否匹配;
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;
// 初始化首行
for(int j = 2; j < n; j += 2)
dp[0][j] = dp[0][j - 2] && p.charAt(j - 1) == '*';
// 状态转移
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);
}
}
边栏推荐
- Switch must be better than if Else fast
- Definition and use of ROS topic messages
- Ini parsing learning documents
- gazebo/set_ model_ State topic driving UAV model through posture
- Arrangement of in-depth learning materials
- MySQL中的InnoDB引擎
- 1.9 - Cache
- Bat 使用细节2
- 【我的创作纪念日】一周年随笔
- Cocos studio3.1 installation package win
猜你喜欢

1.3 - 码制

Connect to remote server

Pay attention to this live broadcast and learn about the path to achieve the dual carbon goal of the energy industry

Graphic octet, really top

Performance comparison of random network, scale-free network, small world network and NS small world matlab simulation

Never forget the original intention, and be lazy if you can: C # operate word files

RT thread migration to s5p4418 (I): scheduler

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

ROS system problem: rosdep init

ETL为什么经常变成ELT甚至LET?
随机推荐
ROS program compilation, like no compilation, refers to the execution of the old compiled executable program
Set in set (III)
Which securities company is good for opening a mobile account? Also, is it safe to open an account online?
When to use redis
ETL为什么经常变成ELT甚至LET?
INI analyse les documents d'apprentissage
Initial love with mqtt
Install the components corresponding to setup
IO streams (common streams)
0基础转行软件测试,如何实现月薪9.5k+
A small template (an abstract class, a complete process is written in a method, the uncertain part is written in the abstract method, and then the subclass inherits the abstract class, and the subclas
[untitled]
c# - C#用fo-dicom对CT图像的PixelData进行处理和转换
tomorrow! "Mobile cloud Cup" competition air publicity will start!
Introduction to programming ape (11) -- structure
File transfer protocol, FTP file sharing server
Ls1028 manual
Docker is equipped with the latest MySQL image
RT thread migration to s5p4418 (III): static memory pool management
Arrangement of in-depth learning materials