题目

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). The function prototype should be:
bool isMatch(const char *s, const char *p) Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

翻译

实现正则表达式中的 .* 匹配

Hints

Related Topics: String, Dynamic Programming, Backtracking

  • 第一个方法就是比较暴力的BF:

    • 比较字符串 s 和 p 的 从 i 和 j 开始的子串是否匹配,用递归的方法直到串的最后,最后回溯得到结果
    • 那么假设现在走到了 s 的 i 位置和 p 的 j 位置:
      • p[j+1]不是 '*',那么判断 s[i] 和 p[j] 是否相同(注意 '.')不同返回 false,相同继续递归下一层 i+1,j+1
      • p[j+1]是 '*',那么看 s[i] 开始的子串,如果 s[i],s[i+1] ... s[i+k] 都等于 p[j],那么它们都可能是合适的匹配,这样递归就要尝试剩下的 (i,j+2),(i+1,j+2),(i+k,j+2)
  • 另一个方法就是动态规划:

    • 通过 dp[len(s)+1][len(p)+1]来存储计算过的结果
    • 遍历的话可以以 p 为外循环,也可以以 s 为外循环
    if p[j] == s[j]:  dp[i][j]=dp[i-1][j-1]
    if p[j] == '.': dp[i][j]=dp[i-1][j-1]
    if p[j] == '*':
    if p[j-1] != s[i]: dp[i][j]=dp[i][j-2] //a*匹配空
    if p[j-1] == s[i] or p[j-1] == '.':
    dp[i][j] = dp[i][j-1] or //a*匹配一个a
    dp[i][j] = dp[i][j-2] or //a*匹配空
    dp[i][j] = dp[i-1][j] //a*匹配两个a

代码

Java

class Solution {
public boolean isMatch(String s, String p) {
if(s.length()==0 && p.length()==0){
return true;
}
if(p.length()==0){
return false;
}
boolean dp[][] = new boolean[s.length()+1][p.length()+1];
dp[0][0] = true;
for(int i=0;i<p.length();i++){
if(p.charAt(i)=='*' && i>0 && dp[0][i-1])
dp[0][i+1] = true;
}
for(int i=0;i<s.length();i++){
for(int j=0;j<p.length();j++){
if(p.charAt(j)=='.')
dp[i+1][j+1] = dp[i][j];
if(p.charAt(j)==s.charAt(i))
dp[i+1][j+1] = dp[i][j];
if(p.charAt(j)=='*'){
if(p.charAt(j-1)!=s.charAt(i) && p.charAt(j-1)!='.'){
dp[i+1][j+1] = dp[i+1][j-1];
}else{
dp[i+1][j+1] = (dp[i+1][j]||dp[i+1][j-1]||dp[i][j+1]);
}
}
}
}
return dp[s.length()][p.length()];
}
}

Python

class Solution(object):
def helper(self, s, p, i, j):
if j == len(p):
return i==len(s)
if j == len(p)-1 or p[j+1]!='*':
if i==len(s) or (s[i]!=p[j] and p[j]!='.'):
return False
else:
return self.helper(s,p,i+1,j+1)
while i<len(s) and (p[j]=='.' or s[i]==p[j]):
if self.helper(s,p,i,j+2):
return True
i += 1
return self.helper(s,p,i,j+2) def isMatch(self, s, p):
return self.helper(s,p,0,0)

蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  7. Java [leetcode 10] Regular Expression Matching

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

  8. [leetcode]10. Regular Expression Matching正则表达式的匹配

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

  9. [LeetCode] 10. Regular Expression Matching ☆☆☆☆☆

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

随机推荐

  1. daily Tip

    daily tip :   <UserControl.Resources>         <Storyboard x:Name="sb1" x:Key=&quo ...

  2. 2017-2018-1 20155315 《信息安全系统设计基础》嵌入式C语言测试

    Hours 要求 伪代码 提取Hours 提取时间地址 时间存放在(基址+2)的16位寄存器中,定义一个时间宏存放地址. #define Time_Addr 0xFFFFC0000 #define T ...

  3. sqlserver 抓取所有执行语句 SQL语句分析 死锁 抓取

    原文:sqlserver 抓取所有执行语句 SQL语句分析 死锁 抓取 在多人开发中最头疼的是人少事多没有时间进行codereview,本来功能都没时间写,哪有时间来开会细细来分析代码.软件能跑就行, ...

  4. JavaEE笔记(六)

    实现Action的几种方法1. implements Action2. extends ActionSupport3. 也可以不继承任何父类不实现任何借口 #当一个类有多个方法 package com ...

  5. exBSGS学习笔记

    exBSGS学习笔记 Tags:数学 题目的话就做下洛谷的模板好了 // luogu-judger-enable-o2 #include<algorithm> #include<io ...

  6. [转载]如何用Visual Studio制作安装包

    原文地址:如何用Visual Studio制作安装包作者:蓝羽幽游 环境:Microsoft Visual Studio 2010 语言:C# 构架:.NET Framework 2.0 解决方案名称 ...

  7. pycharm如何快速替换代码中的字符

    使用快捷键: ctrl+r:快速定位到替换栏,选择要替换的关键字即可方面快捷,特别适用于重复造文字的代码编程上 废话不多少,上图说话硬气:

  8. 阿里云Linux的mysql安装,使用yum安装

    1.下载 我下载的mysql5.7 rpm格式的,在Linux的根目录下下载(防止出现安装的问题) wget https://dev.mysql.com/get/mysql57-community-r ...

  9. 开始试用Dynamics 365

    1. 访问地址: https://trials.dynamics.com/Dynamics365/Signup/ ,默认选择第一个应用,也可以选择其他的,不影响,之后还可以添加更多的应用程序. 2. ...

  10. JavaWeb项目学习教程(2) 系统数据库设计

    最开始本来想写一个管理系统,因为考虑到期末来临,我女朋友就可以看着教程然后学一些东西,然后可以自己慢慢手敲代码.但无奈自己也太懒,两个月过后,我才开始继续写这个博客,而现在我都已经开学了.不过博客还是 ...