[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 ...
随机推荐
- PythonStudy——进制 System of numeration
十进制 人类天然选择了十进制. 二进制 二进制有两个特点:它由两个数码0,1组成,二进制数运算规律是逢二进一. 四进制 四进制是以4为基数的进位制,以 0.1.2 和 3 四个数字表示任何实数. 七进 ...
- 关于动态内存malloc和realloc
1.malloc 1.申请的内存长度可以运行时决定,单位是字节 2.申请的内存为连续的内存空间 3.返回的地址可以根据实际需要强转成对应的类型 4.动态申请内存的生命周期是整个程序,除非手动 ...
- MySQL通过分组计算百分比
公司在做柯米克的分析报告,需要我这边把汽车之家柯米克论坛的评论数据和评论用户所在地的数据获取,通过爬虫的方式很快的解决了数据的问题,但是需要我提取下各省评论人数的比例,所以在数据库里面直接计算了相关的 ...
- Linux touch命令详解
Linux touch命令 Linux touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间.若文件不存在,系统会建立一个新的文件. 用法: touch [-acfm][-d<日 ...
- Fiddler: 如何抓取HTTPS协议的网页
作者:韦玮 转载请注明出处 Fiddler默认只能抓取HTTP协议的网页,不能抓取HTTPS协议的网页,而我们很多时候,都需要抓HTTPS协议的网页,比如抓淘宝数据等.今天,韦玮老师会为大家讲解如何 ...
- Python的socket
第一部分socket的简单示例 服务器部分: """ Description: Author:Nod Date: Record: #------------------- ...
- WPF 后台重写 DataTemplate
/// <summary> /// 配置类 /// </summary> public static class GridControlDeploy { /// <sum ...
- 转: spring静态注入
与其说是静态注入(IOC),不如讲是对JavaBean 的静态成员变量进行赋值. 一般我们在使用依赖注入的时候,如果当前对象(javaBean )创建(实例化)一次,那么非静态的成员变量也会实例化一次 ...
- VS2015+Opencv3.2配置(一次配好)
对于 VS2015+QT5.8的配置我就不介绍了,由于我配置的比较早,具体有的东西忘掉了,大家可以参考下面这几篇文章. 留白留白留白留白留白(稍后补) 对于Opencv+VS的配置是我重点要说的内容. ...
- JavaScript 下拉框 左边添加至右边
关于如何实现右边下拉框中选项的排序一时没有好的解决方法,等想到了回来补充 <!DOCTYPE html> <html> <head> <meta charse ...