当前位置:网站首页>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];
}
}
边栏推荐
- The shell needs to know the commands when running
- webstrom ERROR in [eslint] ESLint is not a constructor
- 【WinForm】关于截图识别数字并计算的桌面程序实现方案
- Find the maximum area of the island -- depth first search (staining method)
- 手工测试如何转向自动化测试?字节5年自动化经验浅谈一下...
- Aruba learning notes 05 configuration architecture WLAN configuration architecture
- 第2章 基礎查詢與排序
- [review of analog electricity - diode]
- ValidationError: Invalid options object. Dev Server has been initialized using an options object th
- Pycharm读取Excel文件时报错:raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+ ; not supported )
猜你喜欢
随机推荐
[baiqihang] Niuer education helps colleges and universities visit enterprises, expand posts and promote employment
Optimisation du serveur Cloud Huawei avec connexion clé
子序列 --- 编辑距离
C language implements StrCmp, strstr, strcat, strcpy
Aruba learning notes 05 configuration architecture WLAN configuration architecture
Consensys Quorum Benchmark Test
Canvas 从入门到劝朋友放弃(图解版)
力扣142题:环形链表2
Excitation generator, monitor
第2章 基础查询与排序
Spotlight light box JS plug-in full screen enlarged picture
Stream stream is used for classification display.
Optimize Huawei ECs to use key login
JS to obtain age through ID card
完全背包!
Shell practice: one click start process, close process and view process status
webstrom ERROR in [eslint] ESLint is not a constructor
STM32输出正弦波+cubeMX配置+HAL库
链表复习!
ValidationError: Invalid options object. Dev Server has been initialized using an options object th


![寻找峰值[抽象二分练习]](/img/99/122e79784f0f07120680d2cbcf89da.png)

![Pycharm读取Excel文件时报错:raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+ ; not supported )](/img/f0/9491ccc2a86d95bb30066397fb9fb6.png)




![[review of analog electricity - diode]](/img/dc/7b3c3187000be24053f0e69733444d.jpg)