题目

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. 2017-2018-1 20155331 课下测试(ch10)

    2017-2018-1 20155331 课下测试(ch10) 假设下面代码中的foobar.txt中有6个ASCII字母,程序的输出是(A) Image 7.png A . c = f B . c ...

  2. Entity Framework 多对多查询的写法

    同学们,看下面的代码段就明白了: 一对多: public ICollection<ReportLookup> GetReportLookup(IEnumerable<Guid> ...

  3. Node.js 从入门到茫然系列——入门篇

    在创建服务的时候,我们一般代码就是: var http = require("http"); var server = http.createServer(function(req ...

  4. JavaEE笔记(六)

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

  5. tomcat : 虚拟路径映射

    tomcat设置虚拟访问地址和真实路径的映射,方法有几种,这里介绍常用的两种方式: 一.修改server.xml文件: 步骤如下:  1.在tomcat根目录下打开conf文件夹,该文件夹下有个ser ...

  6. 传统神经网络ANN训练算法总结

    传统神经网络ANN训练算法总结 学习/训练算法分类 神经网络类型的不同,对应了不同类型的训练/学习算法.因而根据神经网络的分类,总结起来,传统神经网络的学习算法也可以主要分为以下三类: 1)前馈型神经 ...

  7. opencv-Getting Started with Videos

    1.opencv库简单操作视频 # coding = utf-8 # Getting Started with Videos import cv2 import numpy as np # 创建捕获视 ...

  8. 实现后门程序以及相应的rootkits,实现对后门程序的隐藏

    iptables的一些命令: a.    a) 使用规则实现外网不能访问本机,但是本机主动发起的连接正常进行. sudo iptables –A INPUT -p tcp —tcp —syn -j D ...

  9. JZOJ 10043 第k小数

    Description 有两个非负整数数列,元素个数分别为N和M.从两个数列中分别任取一个数相乘,这样一共可以得到NM个数,询问这NM个数中第K小数是多少. 时间限制为20ms . Input 输入文 ...

  10. [BZOJ3167][HEOI2013]SAO[树dp+组合数学]

    题意 给定 \(n\) 个节点和 \(n-1\) 个限制,每个节点有一个权值,每个限制形如:\(a_i< a_j\) ,问有多少个 \(1\) 到 \(n\) 排列满足要求. \(n\leq 1 ...