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:

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-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正则表达式的匹配的更多相关文章

  1. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  2. [LeetCode]10. Regular Expression Matching正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for ...

  3. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  4. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  5. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

  6. 10. Regular Expression Matching正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  7. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

  8. 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]

    题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...

  9. Java [leetcode 10] Regular Expression Matching

    问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

随机推荐

  1. linux下C语言多线程编程实例

    用一个实例.来学习linux下C语言多线程编程实例. 代码目的:通过创建两个线程来实现对一个数的递加.代码: //包含的头文件 #include <pthread.h> #include ...

  2. 小程序 去掉 <button /> 组件默认 border 边框样式

    代码: <button class="btn">点击</button> 效果图: 目的:去掉 <button /> 默认样式中的 backgro ...

  3. linux 磁盘 扩展分区

  4. linux git pull/push时提示输入账号密码之免除设置

    1.先cd到根目录,执行git config --global credential.helper store命令 [root@iZ25mi9h7ayZ ~]# git config --global ...

  5. 关于spring boot在IDE工具中可以启动成功,但是打成jar包以及运行jar包失败的问题

    1. 运行jar包报错,如下图: 2. 首先,找到pom.xml,把下面的build块中的内容改成如下所示: 3. 然后,请千万不要用Intellij idea来打包项目为Jar,你应该来到项目的根目 ...

  6. 7、Curator的常规操作

    package com.ourteam; import org.apache.curator.RetryPolicy;import org.apache.curator.framework.Curat ...

  7. centos密码策略

    centos7密码策略 https://blog.csdn.net/qq_36896749/article/details/80264280 centos7设置密码规则 https://blog.cs ...

  8. css学习1

    1.标签的权值为1,类选择符的权值为10,ID选择符的权值最高为100 注意:还有一个权值比较特殊--继承也有权值但很低,有的文献提出它只有0.1,所以可以理解为继承的权值最低.

  9. 一个linux 4.9,4.14内核的bbr带宽估计偏低问题

    linux 4.9内核,bbr的带宽估计问题. 一个正常的bbr流量图: 对应的ttl图形: 一个异常的bbr流量图: 可以看出,异常的bbr流量图,出现了一个很低的带宽,且稳定在这个带宽10s左右, ...

  10. com.mysql.jdbc.Connection.isValid(I)Z

    spring boot项目 运行提示这个错误的时候 只需要配置