[leetcode]10. Regular Expression Matching正则表达式的匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
Note:
scould be empty and contains only lowercase lettersa-z.pcould be empty and contains only lowercase lettersa-z, and characters like.or*.
Input:
s = "aab"
p = "c*a*b"
Output: true
题意:
'.' any single character.
'*' 0 or more of the preceding element.
whether p can match s ?
Solution1: DP
Step1: 初始化, dp[0][0] = true

初始化, 是否需要预处理第一个row: dp[0][j] ? 发现当S为空,P为'*' 时,P若取0个preceding element就可能变成空,此时两个字符串match。需要预处理。


初始化, 是否需要预处理第一个col:dp[i][0]? 发现当P为空,S为任意字符时,肯定不match。不需要预处理,因为默认default就是false。

Step2: 找到转移方程,
若两个字符串当前的char不同:

若两个字符串当前的char相同:
p.charAt(j-1) == s.charAt(i-1) or p.charAt(j-1) == '.' 则当前字符match, 那么dp[i][j] 的结果可以直接拿dp[i-1][j-1]的取值

若两个字符串当前的char不同:
1. p.charAt(j-1) == '*' 时,先退后两步去check一下T/F。因为 "*" 可以消掉其preceding element,dp[i][j] = dp[i][j-2] 【讨论 '*'代表 0 preceding element 】

2. p.charAt(j-1) == '*' 且 s.charAt(i-1) == p.charAt(j-2) || p.charAt(j-2) == '.' 时 , 则S当前的字符可以看成是 P的
“ precding element + '*' ” 一部分, 此时可以get rid of S当前的字符, dp[i][j] = dp[i-1][j] 【讨论 '*'代表 1 or more preceding element 】

code
class Solution {
public boolean isMatch(String s, String p) {
boolean[][] dp = new boolean[s.length() + 1][p.length() + 1]; // size大小
dp[0][0] = true;
for(int j = 1; j<=p.length(); j++){
if(p.charAt(j-1) == '*') {
dp[0][j] = dp[0][j-2] ;
}
}
for(int i = 1; i<=s.length(); i++){
for(int j = 1; j<=p.length(); j++){
if( p.charAt(j-1) == s.charAt(i-1) || p.charAt(j-1) == '.' ) {
dp[i][j] = dp[i-1][j-1];
}else {
if( p.charAt(j-1) == '*') {
dp[i][j] = dp[i][j-2] ;
if (s.charAt(i-1) == p.charAt(j-2) || p.charAt(j-2) == '.'){
dp[i][j] = dp[i][j] || dp[i-1][j];
}
}
}
}
}
return dp[s.length()][p.length()];//坐标
}
}
注意: 写二维DP,每个人的写code的方法和细节处理不一致。
尤其是为了方便预处理,而多加了空字符' '的二维DP时。
在写code时,很容易弄混到底是dp[s.length()] 还是dp[s.length() + 1]? 到底是 p.charAt(j) 还是 p.charAt(j-1)?
最好的做法是,严格按照自己画的drawing来写,这样不容易出错!
[leetcode]10. Regular Expression Matching正则表达式的匹配的更多相关文章
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode]10. Regular Expression Matching正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- 10. Regular Expression Matching正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
随机推荐
- CSS hack 360浏览器 极速模式与兼容模式
自动切换浏览器模式对于360浏览器7.1版本有效,8.1无效 <%@ Page Language="C#" AutoEventWireup="true" ...
- PythonStudy——运算符优先级 Operator precedence
运算符优先级 以下所列优先级顺序按照从低到高优先级的顺序:同行为相同优先级. 1 Lambda #运算优先级最低 2 逻辑运算符: or 3 逻辑运算符: and 4 逻辑运算符:not 5 成员测试 ...
- jenkins 结合 jmeter 的报告篇
背景:实现jmeter每30分钟执行一次,运行系统中所有接口或者性能脚本或者后续更可以由系统部署来触发建构执行 1.配置jmeter测试环境,注意修改Jmeter的bin目录下jmeter.prope ...
- webpack 中,importloaders 配置项的含义
importLoaders:用于配置「css-loader 作用于 @import 的资源之前」有多少个 loader. 0 => no loaders (default); 1 => p ...
- 比较安全的cookie验证登录设计方案
web是基于HTTP协议传输的,明文传输是极其危险的,随便哪个抓包工具分析下数据包,就over啦,一个加密的传输过程应该包括两部分,一部分为身份认证,用户鉴别这个用户的真伪:另外一部分为数据加密,用于 ...
- mySQL 判断表是否存
select `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_SCHEMA`='数据库名' and `TABLE_NAME`= ...
- Python递归解压缩多级.zip压缩包
参考如下代码(from:https://stackoverflow.com/questions/36285502/how-to-extract-zip-file-recursively-in-pyth ...
- 【Python】hasattr() getattr() setattr() 使用方法详解
本文转自 https://www.cnblogs.com/cenyu/p/5713686.html hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回B ...
- 软件-集成开发环境:IDEA(Java 语言开发的集成环境)
ylbtech-软件-集成开发环境:IDEA(Java 语言开发的集成环境) IDEA 全称IntelliJ IDEA,是用于java语言开发的集成环境(也可用于其他开发语言),IntelliJ在业界 ...
- 如何查看k8s存在etcd中的数据(转)
原文 https://yq.aliyun.com/articles/561888 一直有这个冲动, 想知道kubernetes往etcd里放了哪些数据,是如何组织的. 能看到,才有把握知道它的实现和细 ...