# -*- coding: utf8 -*-
'''
https://oj.leetcode.com/problems/regular-expression-matching/ 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 ===Comments by Dabay===
自我感觉很解得很垃圾啊。如果不是把p做了压缩还过不了online judge。其实就是一个DFA。 当s和p都是空的时候,匹配成功。
如果s为空,p不为空,检查p的偶数为是不是都是*。 如果s和p都不为空,头一个字母
如果不匹配,
不带*,False
带*,递归p[2:]
如果匹配,
不带*,递归s[1:],p[1:]
带*,考虑匹配0到最远端的情况,分别递归s[i:],p[2:]
'''
class Solution:
# @return a boolean
def isMatch(self, s, p):
def compress(p):
i = 0
while i < len(p)-3:
if p[i+1] != '*':
i = i + 1
continue
if p[i+3] == '*':
if p[i] == "." or p[i+2] == ".":
p = p[:i] + ".*" + p[i+4:]
continue
elif p[i] == p[i+2]:
p = p[:i] + p[i+2:]
continue
i = i + 2
return p def isMatch2(s, p):
if len(s) == 0:
if len(p) == 0:
return True
if len(p) % 2 == 0:
i = 1
while i < len(p):
if p[i] != "*":
return False
i = i + 2
else:
return True
else:
return False
if len(s) > 0 and len(p) == 0:
return False match_char = p[0]
multi = False
if len(p) > 1:
if p[1] == "*":
multi = True if match_char != s[0] and match_char != ".":
if multi:
return isMatch2(s, p[2:])
else:
return False if multi is False:
return isMatch2(s[1:], p[1:])
else:
result = False
i = 0
result = isMatch2(s, p[2:])
while i < len(s):
if result == True:
return result
if (s[i] == match_char or match_char == "."):
result = isMatch2(s[i+1:], p[2:])
else:
break
i = i + 1
return result return isMatch2(s, compress(p)) def main():
s = Solution()
print s.isMatch("aa", "a*") if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[LeetCode][Python]Regular Expression Matching的更多相关文章

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

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

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

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

  3. 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]

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

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

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

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

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

  6. [LeetCode] 10. Regular Expression Matching

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

  7. 【leetcode】Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  8. 【leetcode】Regular Expression Matching (hard) ★

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

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

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

随机推荐

  1. Android_使用getIdentifier()获取资源Id

    Android 获取资源ID的另外一种方法,常规获取ID是在特定的文件夹下面的资源,如果在比较特殊的文件夹下面,就需要其他方法获取ID 了: 使用getIdentifier()方法可以方便的获各应用包 ...

  2. 各种类型Android源代码

    商城类APPhttp://community.apicloud.com/bbs/forum.php?mod=viewthread&tid=673&extra=page%3D1 电影影院 ...

  3. CPrimer Plus第12章 存储类、链接和内存管理随笔

    被static修饰的属于内部链接,不可被外部程序文件所使用一般而言,全局变量(文件作用域变量)具有静态存储期,局部变量(代码块作用域变量)具有自动存储期寄存器变量不能使用地址运算符因为被static修 ...

  4. 如何在IIS 中配置应用程序(Convert to Application)?

    1.打开IIS 2.选择待操作的虚拟目录 3.鼠标右键,点击"Convert to Application” 4.点击connect as 5.选中Specific user,并点击Set ...

  5. GitBook整理

    GitBook整理 ECMAScript 6 -- 中文文档 Apache 2.2 --中文官方文档 Redux --React配套架构 英文 express --Node.js 服务端框架 Hexo ...

  6. SQL Server IO系统问题解决

    方法 1. 查询是不是真的要返回这么多的数据. 方法 2. 查询是不是系统的内存不足. 方法 3. 检查查询要访问的数据是不是不常用.如果这个数据不常用,它没有在内存中也就不奇怪了. 方法 4. 是不 ...

  7. Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Chinese_PRC_CI_AS" in the equal to operation.

    Scenario : 这个问题是我的存储过程中用到临时表时发生的. 应该是sql server 服务器的排序规则 (SQL_Latin1_General_CP1_CI_AS ) 与数据库的排序规则(C ...

  8. JNDI support differences between Tibco EMS and ActiveMQ

    Introduction Recently our team was working on Veracity Quick Start sprint, when I was trying to migr ...

  9. Android开发:自定义GridView/ListView数据源

    http://mobile.51cto.com/android-259861.htm 在开发中,我们常常会遇到比较复杂的GridView/ListView的布局,重新实现BaseAdapter不但能帮 ...

  10. DirectUI实现原理

    一,概念 传统的Windows窗口程序对每一个控件都会创建一个句柄,而DUI技术奖所有控件都绘制在一个窗体上,这些控件的逻辑和绘图方式必须自己进行编写和封装,所以这些控件都是无句柄的. DUI技术的实 ...