当前位置:网站首页>leetcode 10. Regular expression matching regular expression matching (difficult)
leetcode 10. Regular expression matching regular expression matching (difficult)
2022-07-05 13:18:00 【InfoQ】
One 、 The main idea of the topic
- 1 <= s.length <= 20
- 1 <= p.length <= 30
- s Only from a-z Lowercase letters of .
- p Only from a-z Lowercase letters of , As well as the character . and *.
- Ensure that characters appear every time * when , All of them are matched with valid characters
Two 、 Their thinking
3、 ... and 、 How to solve the problem
3.1 Java Realization
public class Solution2 {
public boolean isMatch(String s, String p) {
int m = s.length();
int n = p.length();
boolean[][] dp = new boolean[m + 1][n + 1];
dp[0][0] = true;
for (int i = 1; i < n + 1; i++) {
if (p.charAt(i - 1) == '*') {
// asterisk Not on page 1 A place
dp[0][i] = dp[0][i - 2];
}
}
for (int i = 1; i < m + 1; i++) {
for (int j = 1; j < n + 1; j++) {
// The last wildcard may be Order number 、 asterisk 、 character
if (p.charAt(j - 1) == '.') {
// The last wildcard is Order number
dp[i][j] = dp[i - 1][j - 1];
} else if (p.charAt(j - 1) != '*') {
// The last wildcard is not asterisk
dp[i][j] = dp[i - 1][j - 1] && p.charAt(j - 1) == s.charAt(i - 1);
} else if (p.charAt(j - 2) != s.charAt(i - 1) && p.charAt(j - 2) != '.') {
dp[i][j] = dp[i][j - 2];
} else {
dp[i][j] = dp[i][j - 1] || dp[i - 1][j] || dp[i][j - 2];
}
}
}
return dp[m][n];
}
}
Four 、 Summary notes
- 2022/7/5 people follow the example of their superiors , The emperor's new dress
边栏推荐
- 先写API文档还是先写代码?
- MySQL --- 数据库查询 - 排序查询、分页查询
- 946. 验证栈序列
- MySQL 巨坑:update 更新慎用影响行数做判断!!!
- JS to determine whether an element exists in the array (four methods)
- 前缀、中缀、后缀表达式「建议收藏」
- How to protect user privacy without password authentication?
- 《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
- Android本地Sqlite数据库的备份和还原
- Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications
猜你喜欢
Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function
数据泄露怎么办?'华生·K'7招消灭安全威胁
什么是网络端口
Detailed explanation of navigation component of openharmony application development
OpenHarmony应用开发之Navigation组件详解
简单上手的页面请求和解析案例
LeetCode20.有效的括号
《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
Lb10s-asemi rectifier bridge lb10s
DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
随机推荐
Default parameters of function & multiple methods of function parameters
阿里云SLB负载均衡产品基本概念与购买流程
Difference between avc1 and H264
程序员成长第八篇:做好测试工作
Changing JS code has no effect
#yyds干货盘点# 解决名企真题:搬圆桌
RHCSA9
go map
MSTP and eth trunk
使用Dom4j解析XML
爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
Developers, is cloud native database the future?
CAN和CAN FD
Actual combat simulation │ JWT login authentication
Get you started with Apache pseudo static configuration
《2022年中國銀行業RPA供應商實力矩陣分析》研究報告正式啟動
Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
STM32 and motor development (from architecture diagram to documentation)
MATLAB论文图表标准格式输出(干货)
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例