题目来源:

https://leetcode.com/problems/regular-expression-matching/


题意分析:

这道题目定义了两个正则表达式规则。’.’代表任意字符,’*’代表前一个字符出现任意次。输入两个字符串s,p。如果s可以被p完全匹配则返回True,否则返回False。比如’.*’可以匹配任意字符串。


题目思路:

这道题目如果直接import 正则表达式肯定是不行的,因为题目只定义了2个特殊字符,而正则表达式包括很多特殊字符,所以直接import正则表达式肯定是不行的

从第一个字符开始搜索,对以下情况进行讨论:

①如果(s[0] == p[0]or p[0] == ‘.’)且p的第二个字符不是’*’,那么如果两个字符同时去掉第一个字符,结果不变。

②如果p[0] == ‘.’且p[1] == ‘*’那么如果存在s[i:],i >= 0被p[2:]完全匹配,则s可以被p完全匹配(s[i:]代表s字符除掉0到i下标的字符)。

③如果p[0] != ‘.’且p[1] == ‘*’,那么如果s[0] != p[0]那么如果s可以被p[2:]匹配,则s可以被p完全匹配。

④如果s[0] == p[0]那么如果存在s[i:](i >= 0 且 s[j] == p[0] 0<= j <i)被p[2:]完全匹配,那么s可以被p完全匹配。

经过上面的分析,这道题目可以用动态规划来做。用b[i][j]来代表由s除掉0到i下标的字符能否被由p除掉0到j下标的字符完全匹配,也就是s[i:]能否被p[j:]完全匹配。根据上一段的分析可以得到:

if p[j + 1] != ‘*’

if s[i] == p[j] or p[j] == ‘.’ then b[i][j] = b[i + 1][j + 1]

fi

else

if p[j] == ‘.’ then

if exist k (i<=k) b[k][j + 2] = true then b[i][j] = true

else b[i][j] = false

fi

else

if exist k(i <= k)b[k][j + 2] = true and all l (0 <= l <k) s[l] = p[0] then b[i][j] =true

else b[i][j] = false

fi

fi

return b[0][0]

PS:由于python的二维数组初始化比较麻烦,或者是因为我对python的不熟,我选择用dictionary来处理,b[i,j]代替b[i][j].


代码(python):

 class Solution(object):
def isMatch(self,s, p):
size1 = len(s)
size2 = len(p)
b = {}
s1 = 0
while s1 <= size1:
s2 = 0
while s2 <= size2:
b[s1,s2] = False
s2 += 1
s1 += 1
b[size1,size2] = True
j = size2 - 1
while j >= 0:
if p[j] == '*':
b[size1,j] = False
j -= 1
b[size1,j] = True
j -= 1
else:
break
j = size2 - 1
while j >= 0:
if p[j] == '.':
i = size1 - 1
while i >= 0:
b[i,j] = b[i + 1,j + 1]
i -= 1
elif p[j] != '*':
i = size1 - 1
while i >= 0:
if p[j] == s[i]:
b[i,j] = b[i + 1,j + 1]
else:
b[i,j] = False
i -= 1
else:
i = size1 - 1
if p[j - 1] == '.':
while i >= 0:
k = i
while k <= size1:
if b[k,j + 1]:
b[i,j - 1] =True
break
k += 1
i -= 1
else:
while i >= 0:
k = i
while k <= size1:
if b[k,j + 1]:
b[i,j - 1] = True
break
if k == size1:
break
if p[j - 1] != s[k]:
break
k +=1
i -= 1
j -= 1
j -= 1
return b[0,0]

转载请注明出处:http://www.cnblogs.com/chruny/p/4807234.html

[LeetCode]题解(python):010-Regular Expression Matching的更多相关文章

  1. [Leetcode][Python][DP]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...

  2. 【LeetCode】010. Regular Expression Matching

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

  3. 【JAVA、C++】LeetCode 010 Regular Expression Matching

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

  4. LeetCode 010 Regular Expression Matching

    题目描述:Regular Expression Matching Implement regular expression matching with support for '.' and '*' ...

  5. 010 Regular Expression Matching 正则表达式匹配

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

  6. No.010 Regular Expression Matching

    10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...

  7. leetcode第十题--Regular Expression Matching

    Problem:Implement regular expression matching with support for '.' and '*'. '.' Matches any single c ...

  8. LeetCode--No.010 Regular Expression Matching

    10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...

  9. LeetCode(10) Regular Expression Matching

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

  10. LeetCode(10)Regular Expression Matching

    题目如下: Python代码: # -*- coding:utf-8 -*- def ismatch(s,p): #先将dp[s+1][p+1]二维数组全置为False dp = [[False] * ...

随机推荐

  1. IQueryable与IEnumberable的区别(转)

    转自 http://www.cnblogs.com/fly_dragon/archive/2011/02/21/1959933.html IEnumerable接口 公开枚举器,该枚举器支持在指定类型 ...

  2. sql显示12个月数据

    需求 最近在做一个财务报表展示系统,Budget需要当月上传,还未上传月份的数据也需要显示出来. 数据库设计 cBudget表结构如下 CREATE TABLE [dbo].[cBudget]( ,) ...

  3. Android 打开系统最近任务及最近应用方法

    Class serviceManagerClass; try { serviceManagerClass = Class.forName("android.os.ServiceManager ...

  4. jquery中的on事件

    on()函数用于为指定元素的一个或多个事件绑定事件处理函数. 从jQuery 1.7开始,on()函数提供了绑定事件处理程序所需的所有功能,用于统一取代以前的bind(). delegate(). l ...

  5. 自学HTML5第三节(拖放效果)

    今天来看看网页上的拖放效果,首先来看看什么是拖放———— 拖放 拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. 浏览器支持 Inte ...

  6. 从头开发MUDLIB

    跟Akuma一起从头打造mudlib--[第一讲] 第一讲:让它跑起来注:每一讲我都会上传一个相符的lib,有些文件是旧的,有些是新的,我尽可能在lib里写清楚注释.更详细的内容则在每讲的正文里写. ...

  7. JQuery DataTables Editor---只修改页面内容

    近来在工作中需要对JQuery DataTables进行增,删,改的操作,在网上找了一些资料,感觉比较的好的就是(http://editor.datatables.net/)文章中所展示的操作方法(如 ...

  8. Android源码下载

    Android源码下载 1.安装git 2.安装repo 从这里 https://dl-ssl.google.com/dl/googlesource/git-repo/repo 下载repo文件 3. ...

  9. Windows DDB和DIB技术应用(3)--图元外边矩形检测

    GDI/GDI+中只有对字体的外边框的测量,而没有提供对点,线,面,曲线的外边框获取函数.下面是本人利用DIB技术编写的探测简单图元,甚至也可以探测自己定义的复杂图元的外边矩形框的函数.本人已经测试, ...

  10. Codeforces 339E

    #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> # ...