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. uip移植telnetd并加入自己定义命令

    版权声明: https://blog.csdn.net/cp1300/article/details/30541507 刚刚移植了一下uip的telnetd,还是比較简单方便的. 首先加入文件,注意u ...

  2. (转)python logging模块

    python logging模块 原文:http://www.cnblogs.com/dahu-daqing/p/7040764.html 1 logging模块简介 logging模块是Python ...

  3. Web APP 日期选择控件

    github地址: https://github.com/yuanzm/simple-date-picker#simple-date-picker simple-date-picker 基于zepto ...

  4. 5G投资逻辑

    5G投资逻辑 关注光模块生产厂商. 通信射频滤波器,功率放大器生产厂商. 光无源器件的需求增多

  5. Delphi操作剪贴板

    Windows使用剪贴板观察器和观察链.剪贴板观察器是一个显示剪贴板当前内容的窗口.            通常它应该至少能显示三种普通格式的内容:文字CF_TEXT.位图CF_BITMAP.元文件C ...

  6. JAVA常用工具类异常处理

    1异常的定义 异常就是与我们编译相违背在过程中出现的逻辑或忘记一些赋值等等 分为编译时错误和运行时错误 运行时异常 我们一般处理的时Exception异常: 异常处理 异常处理可以通过关键字try,c ...

  7. 文件处理,三元操作符,seek()函数,迭代函数和列表解析,reduce函数

    1.文件读取方类型 r,r+,w,x,a, r,读文件 w,写文件,文件内容全部删除,并将新内容从第一行开始赋值 x,写文件,只有文件不存在,可写,文件存在,报错 a,在文件莫问追加信息 r+,w+, ...

  8. redis总结问题

    简单回顾了redis,在这过程中 首先得了解redis是什么,redis的运用场景,redis支持哪些数据格式,redis如何操作数据,redis如何实现高可用 redis是什么: Redis 是一个 ...

  9. 如何让Excel单元格中的名字分散对齐

    1 操作方式 开始->对齐方式->对齐->水平对齐->分散对齐(缩进) 2 优势 不会破坏数据的有效性

  10. keil5 MDK warning:registered ARM compiler version not found in path

    重装 打开keil5弹出窗口: warning:registered ARM compiler version not found in path... 解决: 增加系统环境变量 ARMCC5LIB ...