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. PythonStudy——列表与字典推导式 List and dictionary derivation

    # 快速生成列表或字典的语法糖,且能在生成过程中添加简单的逻辑 # 能被列表推导式推导的数据源必须在循环取值时可以得到一个值 ls = [v for v in range(1, 6)] print(l ...

  2. Spring各个jar包作用

    Spring AOP:Spring的面向切面编程,提供AOP(面向切面编程)的实现Spring Aspects:Spring提供的对AspectJ框架的整合Spring Beans:Spring IO ...

  3. k8s-YAML配置文件(转)

    转自http://www.cnblogs.com/bigberg/p/9203619.html 一.YAML基础 YAML是专门用来写配置文件的语言,非常简洁和强大,使用比json更方便.它实质上是一 ...

  4. 解决Android Studio在Ubuntu上出现“sdk/platform-tools/adb: error=2, No such file or directory”的方法

    转载至http://blog.163.com/china_uv/blog/static/11713726720136931132385/ 刚安装Ubuntu14.5时运行Android Studio可 ...

  5. Python脚本模拟僵尸进程与孤儿进程

    最近一台机器的systemd内存高达30%多,一直不变,后来排查是僵尸进程,什么是僵尸进程呢,只能google,百度等先了解,然后自己总结了一下,虽然这是基础的东西,但是对于我来说就如新大陆一样.花了 ...

  6. 《Linux内核原理与设计》第五周作业

    使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 方法一:使用库函数API在屏幕上显示进程的ID 先在实验楼中打开XFCE,在目录下输入指令: vi getpid.c;新建并打开get ...

  7. 二十一、springcloud(七)服务网关zuul

    1.简介 Eureka用于服务的注册于发现,Feign支持服务的调用以及均衡负载,Hystrix处理服务的熔断防止故障扩散,Spring Cloud Config服务集群配置中心,在微服务架构中,后端 ...

  8. 第二章 C#语法基础 (2.2 C#语言的运算符和表达式)

    [案例]本案例通过随机数发生器随机产生三条边,要求输出三天边长(边长长度为1~20的整数),并判断是否可以构成一个三角形. 如果可以,则计算出三角形面积,否则输出信息”三条随机的边不能构成三角形“. ...

  9. 使用pageoffice进行多个文档的合并

    提前给test模板文件中 手动插入一个书签,因为pageoffice必须有一个书签后,才能在后台进行书签的创建 //多个word文件进行合并 string strCopyFolder = System ...

  10. 【fork/join】java并发编程-fork/join示例

    package com.chinamobile.epic.tako.common.graphite.query.sync.impl; import com.google.common.collect. ...