当前位置:网站首页>剑指offer19 正则表达式
剑指offer19 正则表达式
2022-07-23 08:03:00 【ATTACH_Fine】
题目
请实现一个函数用来匹配包含’. ‘和’‘的正则表达式。**模式中的字符’.‘表示任意一个字符,而’'表示它前面的字符可以出现任意次(含0次)**。在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但与"aa.a"和"ab*a"均不匹配。
示例:
设 s 的长度为 n , p 的长度为 m ;将s 的第 i 个字符记为 s_i ,p 的第 j 个字符记为 p_j ,将 s 的前 ii个字符组成的子字符串记为 s[:i] , 同理将 p 的前 j 个字符组成的子字符串记为 p[:j] 。
-------> 可转化为求s[:n] 是否能和 p[:m] 匹配。
-------->是从 s[:1] 和 p[:1] 是否能匹配开始判断,每轮添加一个字符并判断是否能匹配,直至添加完整个字符串 s和 p 。展开来看,假设 s[:i] 与 p[:j]可以匹配,那么下一状态有两种:
1.添加一个字符 s_{i+1} 后是否能匹配?
2.添加字符 p_{j+1}后是否能匹配?
动态规划解析
状态定义: 设动态规划矩阵 dp , dp[i][j] 代表字符串 s 的前 i 个字符和 p 的前 j 个字符能否匹配。
转移方程:由于 dp[0][0] 代表的是空字符的状态, 因此 dp[i][j] 对应的添加字符是 s[i - 1] 和 p[j - 1]
初始化:
初始化: 需要先初始化 dp 矩阵首行,以避免状态转移时索引越界。
dp[0][0] = true: 代表两个空字符串能够匹配。
dp[0][j] = dp[0][j - 2] 且 p[j - 1] = ‘*’: 首行 s 为空字符串,因此当 p 的偶数位为 * 时才能够匹配(即让 p 的奇数位出现 0 次,保持 p 是空字符串)。因此,循环遍历字符串 p ,步长为 2(即只看偶数位)。
代码
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;
else if(dp[i-1][j] && s.charAt(i-1) == p.charAt(j-2)) dp[i][j] = true;
else if(dp[i-1][j] && p.charAt(j-2) == '.') dp[i][j] = true;
}else{
if(dp[i-1][j-1] && s.charAt(i-1) == p.charAt(j-1)) dp[i][j] = true;
else if(dp[i-1][j-1] && p.charAt(j-1) == '.' ) dp[i][j] = true;
}
}
}
return dp[m-1][n-1];
}
}
边栏推荐
- 网络安全笔记1——Internet协议的安全性
- How to judge whether an object is empty
- rtx3080相当于gtx什么显卡 rtx3080显卡什么水平 rtx3080显卡怎么样
- Principle of container network
- Uiscrollview (uicollectionview) prohibits horizontal and vertical sliding at the same time
- kafka消费报错coordinator unavailable.Rediscovery will be attempt redisCovery
- 第五天实验
- 天玑920相当于骁龙什么 天玑920相当于骁龙多少 天玑920怎么样
- rtx3080ti和3090差距 rtx3080ti和3090哪个性价比高
- Day108. Shang Yitong: interface docking of hospital simulation system - query of hospital | Department | shift scheduling, addition, deletion, modification and paging conditions
猜你喜欢
随机推荐
js 实现 encode64 加密
Where does pytorch work?
How about Celeron n5095 Processor? What is the equivalent level of Intel n5095 core display
How to open the thought map pdf of postgraduate entrance examination in the small program of postgraduate entrance examination question bank
Detailed explanation of knapsack problem
Day 11 notes
拖拽----
How many processors is Tianji 1100 equivalent to snapdragon? How about Tianji 1100 equivalent to snapdragon
[baiqihang] Niuer education helps colleges and universities visit enterprises, expand posts and promote employment
What level of rtx3070ti graphics card? What level of rtx3070ti graphics card? How about rtx3070ti graphics card
Pbootcms数据库转换教程(sqlite转mysql详细教程)
Using redis to realize distributed lock (single redis)
Which is the difference between iqoo 10 pro and vivo X80 pro? Detailed parameter configuration comparison
-bash: ifconfig: command not found
第四天笔记
采样和数据驱动
Configure the firetracker process, i.e. stepping on the pit record
How to judge whether an object is empty
赛扬n5095处理器怎么样 英特尔n5095核显相当于什么水平
Comparison of iqoo 10 pro and Xiaomi 12 ultra configurations









