当前位置:网站首页>Sword finger offer19 regular expression
Sword finger offer19 regular expression
2022-07-23 14:32:00 【ATTACH_ Fine】
subject
Please implement a function to match the contains ’. ‘ and ’‘ Regular expression of .** Characters in pattern ’.‘ Represents any character , and ’' Indicates that the character in front of it can appear any number of times ( contain 0 Time )**. In the subject , Matching means that all characters of a string match the whole pattern . for example , character string "aaa" With the model "a.a" and "abaca" matching , But with "aa.a" and "ab*a" No match .
Example :
set up s The length of is n , p The length of is m ; take s Of the i Characters are marked as s_i ,p Of the j Characters are marked as p_j , take s Before ii The substring composed of characters is recorded as s[:i] , In the same way p Before j The substring composed of characters is recorded as p[:j] .
-------> It can be transformed into seeking s[:n] Can you and p[:m] matching .
--------> It's from s[:1] and p[:1] Whether it can match starts to judge , Add one character per round and judge whether it can match , Until the complete string is added s and p . Expand to see , hypothesis s[:i] And p[:j] Can match , Then there are two states :
1. Add a character s_{i+1} Whether it can match ?
2. Add characters p_{j+1} Whether it can match ?
Dynamic planning analysis
State definition : Let's set the dynamic programming matrix dp , dp[i][j] Representative string s Before i Characters and p Before j Whether the characters match .
Transfer equation : 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]
initialization :
initialization : You need to initialize dp First row of matrix , To avoid index out of bounds during state transition .
dp[0][0] = true: Represents that two empty strings can match .
dp[0][j] = dp[0][j - 2] And p[j - 1] = ‘*’: First line s Is an empty string , So when p The even digits of are * Can match ( That is to let p The odd digits appear 0 Time , keep p Is an empty string ). therefore , Loop through the string p , In steps of 2( That is, only look at even digits ).
Code
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;
// initialization
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];
}
}
边栏推荐
- ValidationError: Invalid options object. Dev Server has been initialized using an options object th
- 多重背包!
- 【WinForm】关于截图识别数字并计算的桌面程序实现方案
- Question 142 of Li Kou: circular linked list 2
- JS数据类型判断方式总结
- 鸡与蛋,产品与策略
- Vk36n5d anti power interference / mobile phone interference 5-key 5-channel touch detection chip anti freeze function ponding in the touch area can still be operated
- Chapter 2 basic query and sorting
- 视觉与智能学习近期期刊阅读与相关知识学习
- 第2章 基础查询与排序
猜你喜欢
![[download attached] several scripts commonly used in penetration testing that are worth collecting](/img/01/3b74c5ab4168059827230578753be5.png)
[download attached] several scripts commonly used in penetration testing that are worth collecting

LZ77文件压缩

C language implementation of classroom random roll call system

几种点云(网格)孔洞填充方法(1)

求岛屿最大面积--深度优先搜索(染色法)

OKRK3399开发板预留I2C4挂载EEPROM

How can manual testing turn to automated testing? Byte 5 years of automation experience talk about

Flat style feedback form page

回文相关题目

uni-app知识点和项目上遇到的问题和解决办法的记录
随机推荐
工作小记:一次抓包
Day 5 experiment
单调栈!!!
右键新建txt,新建文本文件不见了,通过添加注册表就可以解决,找来找去办法解决不了的终极办法
股票炒股开户风险性大吗,安全吗?
云呐|怎样管理固定资产?如何进行固定资产管理?
云呐|公司固定资产如何管理?公司固定资产如何管理比较好?
LZ77文件压缩
Experience in developing large crawlers in requests Library
Canvas from getting started to persuading friends to give up (graphic version)
STM32 output sine wave +cubemx configuration +hal Library
【论文笔记】基于分层深度强化学习的移动机器人导航方法
【我可以做你的第一个项目吗?】GZIP的细节简介和模拟实现
云呐-如何加强固定资产管理?怎么加强固定资产管理?
LabVIEW运行中改变Chart的历史长度
ThreadLocal interview Kills 11 consecutive questions
Shell practice: one click start process, close process and view process status
Interface
581. 最短无序连续子数组
STM32输出SPWM波,HAL库,cubeMX配置,滤波后输出1KHz正弦波